Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

c# - UnassignedReferenceException when accessing variable

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?

I get this Error:

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/G??ameObjectBindings.ge??n.cs:35) ObstacleController.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets / esplades / Scripts / ObstacleController.cs: 52)

Original Script.

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

(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;
        //------------------------------------------
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can turn off the isKinematic of Rigidbody2D from the isKinematickstop GameObject with isKinematickstop.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;
        //------------------------------------------
    }
}

EDIT:

With the UnassignedReferenceException: error:

You have to assign the GameObject to the isKinematickstop slot.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...