访问变量时出现UnassignedReferenceException [英] UnassignedReferenceException when accessing variable
问题描述
我要在此代码中插入停止代码.但是我做不到.如何关闭isKinematicStop对象的isKinematic功能?
I want to insert the stop code into this code. But I could not do it. How can I turn off the isKinematic feature of isKinematicStop object?
我收到此错误:
我收到此错误UnassignedReferenceException:变量 尚未分配ObstacleController的isKnematickstop.你 可能需要将isKinematickstop变量分配给 检查器中的ObstacleController脚本. UnityEngine.GameObject.GetComponent [Rigidbody2D]()(在C: /buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:35) ObstacleController.OnCollisionEnter2D(UnityEngine.Collision2D col) (位于Assets/esplades/Scripts/ObstacleController.cs:52)
I get this error UnassignedReferenceException: The variable isKnematickstop of ObstacleController has not been assigned. You probably need to assign the isKinematickstop variable to the ObstacleController script in the inspector. UnityEngine.GameObject.GetComponent [Rigidbody2D] () (at C: /buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:35) ObstacleController.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets / esplades / Scripts / ObstacleController.cs: 52)
原始脚本.
using UnityEngine;
using System.Collections;
public class ObstacleController : MonoBehaviour
{
public float hitPushBack;
public GameObject hitEffect;
public Sprite[] sprites;
public void Awake()
{
if (sprites.Length > 0)
GetComponent<SpriteRenderer>().sprite = sprites[Random.Range(0, sprites.Length)];
}
void OnEnable()
{
GameManager.GameStateChanged += OnGameStateChanged;
}
private void OnGameStateChanged(GameState newState, GameState oldState)
{
if (newState == GameState.GameOver)
gameObject.SetActive(false);
}
void OnDisable()
{
GameManager.GameStateChanged -= OnGameStateChanged;
}
void Update()
{
//Quaternion targetRotation = Quaternion.Euler(0, 0, RotationVariables.direction * Mathf.Abs(RotationVariables.maxAngle));
//transform.root.rotation = Quaternion.RotateTowards(transform.root.rotation, targetRotation, RotationVariables.rotationDelta);
}
public void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.tag == "Player")
{
hitEffect.transform.position = col.contacts[0].point;
hitEffect.gameObject.SetActive(true);
GameManager.Instance.playerController.anim.Squeeze();
GameManager.Instance.playerRigidbody.AddForce(col.contacts[0].normal * hitPushBack);
}
}
}
isKinematickstop.GetComponent()中的
(Col.collider.tag =="Player"). IsKinematic =假;我该如何运行? 我做到了.
(Col.collider.tag == "Player") in isKinematickstop.GetComponent (). IsKinematic = false; How can I run it? I did it..
using UnityEngine;
using System.Collections;
public class ObstacleController : MonoBehaviour
{
public float hitPushBack;
public GameObject hitEffect;
public Sprite[] sprites;
//------------------------------------------
public GameObject isKinematickstop;
//------------------------------------------
public void Awake()
{
if (sprites.Length > 0)
GetComponent<SpriteRenderer>().sprite = sprites[Random.Range(0, sprites.Length)];
}
void OnEnable()
{
GameManager.GameStateChanged += OnGameStateChanged;
}
private void OnGameStateChanged(GameState newState, GameState oldState)
{
if (newState == GameState.GameOver)
gameObject.SetActive(false);
}
void OnDisable()
{
GameManager.GameStateChanged -= OnGameStateChanged;
}
void Update()
{
//Quaternion targetRotation = Quaternion.Euler(0, 0, RotationVariables.direction * Mathf.Abs(RotationVariables.maxAngle));
//transform.root.rotation = Quaternion.RotateTowards(transform.root.rotation, targetRotation, RotationVariables.rotationDelta);
}
public void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.tag == "Player")
{
hitEffect.transform.position = col.contacts[0].point;
hitEffect.gameObject.SetActive(true);
GameManager.Instance.playerController.anim.Squeeze();
//------------------------------------------
isKinematickstop.GetComponent<Rigidbody2D>().isKinematic= false;
//------------------------------------------
}
}
}
推荐答案
您可以使用isKinematickstop.GetComponent<Rigidbody2D>().isKinematic = false;
从isKinematickstop GameObject中关闭Rigidbody2D的isKinematic
.
You can turn off the isKinematic
of Rigidbody2D from the isKinematickstop GameObject with isKinematickstop.GetComponent<Rigidbody2D>().isKinematic = false;
.
我注意到您已经这样做了.我相信您想在进入触发器的GameObect上执行此操作.如果是这样,那么col.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
应该这样做.
I noticed that you have already done that. I believe that you want to do this on the GameObect that enters the trigger. If that's true then col.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
should do it.
public void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.CompareTag("Player"))
{
hitEffect.transform.position = col.contacts[0].point;
hitEffect.gameObject.SetActive(true);
GameManager.Instance.playerController.anim.Squeeze();
//------------------------------------------
col.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
//------------------------------------------
}
}
带有UnassignedReferenceException:错误:
With the UnassignedReferenceException: error:
您必须将GameObject分配给isKinematickstop插槽.
You have to assign the GameObject to the isKinematickstop slot.
这篇关于访问变量时出现UnassignedReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!