0
votes

I am making a 2D game where a ball keeps instantiating with a color red, green, blue or yellow. There is a square paddle with all the 4 colors. if the ball hits the same color it should destroy itself. I have made a script for instantiating the object.

This is that script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ballSpawn : MonoBehaviour {

    public GameObject ball;
    public Transform ballSpawnTransform;
    private float startTime = 2f; 
    private float repeatRate = 2f;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("BallSpawn", startTime, repeatRate);
    }

    // Update is called once per frame
    void Update () {

    }

    public void BallSpawn(){
        ball = Instantiate (ball, ballSpawnTransform.position, ballSpawnTransform.rotation);
    }
}

I have attached the color change method and destroy method with the prefab. the script for that is provided below:-

public class ball_controller : MonoBehaviour {

    [SerializeField]
    public int colorInt;
    Rigidbody2D rb;

    // Use this for initialization
    void Start () {
        //rb = this.gameObject;
        colorInt = Random.Range (1, 5);
        switch (colorInt) {
        case 1:
            gameObject.GetComponent<Renderer> ().material.color = Color.red;
            break;
        case 2:
            gameObject.GetComponent<Renderer> ().material.color = Color.green;
            break;
        case 3:
            gameObject.GetComponent<Renderer> ().material.color = Color.blue;
            break;
        case 4:
            gameObject.GetComponent<Renderer> ().material.color = Color.yellow;
            break;
        }
        rb = this.gameObject.GetComponent<Rigidbody2D> ();
        rb.velocity = new Vector3 (0f, -5f, 0f);
    }

    // Update is called once per frame
    void Update () {

    }

    void OnTriggerEnter2D(Collider2D hit){
        if (hit.gameObject.GetComponent<Renderer> ().material.color == gameObject.GetComponent<Renderer> ().material.color) {
            Destroy (this.gameObject);
        } else {
            DestroyObject (this.gameObject);
        }

    }
}

but when it destroys the game object. It gives the following error .

"MissingReferenceException: The object of type 'GameObject' 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."

It does not instantiate anymore balls and keeps giving this error for every ball it should have initiated. I want my script to keep instantiating clones.

1
What does DestroyObject() do?Andrea

1 Answers

3
votes

I believe this may be because you are overwriting ball with the newly instantiated ball. Once that ball is destroyed the reference to that component is broken. You should instead have two seperate Gameobject variables - one to hold the ball prefab, and one to hold the reference to a new ball in your script:

public GameObject ball; //the prefab you want to spawn
private GameObject spawnedball; //a local temporary reference

public void BallSpawn(){
    spawnedball = Instantiate (ball, ballSpawnTransform.position, ballSpawnTransform.rotation);
}