Invalid button click

I don’t know what I am doing wrong. There are two objects in a 2D project. One has a component RigidBody2Dand BoxCollider2D. The second object has only BoxCollider2D. And at the bottom there is a button when the button Object1again falls on Object2and Destroyand Instantiate Object1. But when Object1 Instantiate, then pressing the button does not work. And the error occurred as follows:

The object of type Rigidbody2D has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

Object 1:

enter image description here

Object 2:

enter image description here

Button

Click:

enter image description here

Object 1 Script:

public class Object1 : MonoBehaviour {

    public static Object1 instance;

    [SerializeField]
    private Rigidbody2D body;

    [SerializeField]
    private bool hasdropped;

    [SerializeField]
    private bool click;

    [SerializeField]
    private float PointerPos;

    [SerializeField]
    private float BorderX;

    void Awake(){

        if (instance == null) {

            instance = this;
        }

        //take width of screen
        Vector3 gameScreen = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height,0));

        BorderX = gameScreen.x - 0.6f;




        body.isKinematic = true;
        click = true;
        hasdropped = true;


    }



    void FixedUpdate () {

        if (click) {

            Vector3 temp = transform.position;

            PointerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

            temp.x = Mathf.Clamp(PointerPos,-BorderX,BorderX);

            body.position = temp;

            if(hasdropped){
                return;

            }

        }


    }

    public void ButtonClick(){

        body.isKinematic = false;

    }


}

Object 2 Script:

public class Object2 : MonoBehaviour {

    [SerializeField]
    private GameObject BallClone;




    void OnCollisionEnter2D(Collision2D target){

        Destroy (target.gameObject);

        Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity);


    }

}
+4
source share
1 answer

The problem is that you referenced Object1Prefab (a living object, not an assembly) on ClickButton On Click (). (But the link to the collection will not work at all)

, . (: ClickButton , script, ? . ClickButton ... .)

ButtonClick() 2 ( ):

using UnityEngine;
using System.Collections;

public class Object2 : MonoBehaviour {

    [SerializeField]
    private GameObject BallClone;

    public GameObject mine;


    void OnCollisionEnter2D(Collision2D target){

        Destroy (target.gameObject);

        mine = Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity) as GameObject;


    }




public void ButtonClick(){
//this is just an example. You might wanna to cache this info for better practice and not call GetComponent all the time
            mine.GetComponent<Rigidbody2D> ().isKinematic = false;
        }




}

1- GameObject1Prefab ( ) Object2 script "mine". public GameObject mine.

, , :

mine = Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity) as GameObject;

ClickButton GameObject2, 1, On Click().

? .: ~

0

Source: https://habr.com/ru/post/1618588/


All Articles