Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...