访问变量时出现UnassignedReferenceException

时间:2017-04-17 16:07:10

标签: c# unity3d

我想在此代码中插入停止代码。但我不能这样做。如何关闭isKinematicStop对象的isKinematic功能?

我收到此错误:

  

我收到此错误UnassignedReferenceException:变量   isknematickstop of ObstacleController尚未分配。您   可能需要将isKinematickstop变量赋值给   检查器中的ObstacleController脚本。   UnityEngine.GameObject.GetComponent [Rigidbody2D]()(在C:   / buildslave /团结/建造/伪影/生成/普通/运行/ G ameObjectBindings.ge n.cs:35)   ObstacleController.OnCollisionEnter2D(UnityEngine.Collision2D col)   (在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 = false;我该怎么办呢? 我做到了..

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;
        //------------------------------------------
    }
}
}

1 个答案:

答案 0 :(得分:2)

您可以使用isKinematic从isKinematickstop GameObject关闭Rigidbody2D的isKinematickstop.GetComponent<Rigidbody2D>().isKinematic = false;

我注意到你已经做到了。我相信你想在进入触发器的GameObect上做这件事。如果那是真的那么col.gameObject.GetComponent<Rigidbody2D>().isKinematic = false;就应该这样做。

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:错误:

您必须将GameObject分配给isKinematickstop插槽。

enter image description here