C# 当实例化的游戏对象被销毁时,它不再产生克隆

C# 当实例化的游戏对象被销毁时,它不再产生克隆,c#,unity3d,rigid-bodies,C#,Unity3d,Rigid Bodies,我正在制作一个2D游戏,其中一个球不断以红色、绿色、蓝色或黄色实例化。有一个四种颜色的方形桨。如果球击中同一颜色,它就会自毁。我已经制作了一个用于实例化对象的脚本 这就是脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ballSpawn : MonoBehaviour { public GameObject ball; public

我正在制作一个2D游戏,其中一个球不断以红色、绿色、蓝色或黄色实例化。有一个四种颜色的方形桨。如果球击中同一颜色,它就会自毁。我已经制作了一个用于实例化对象的脚本

这就是脚本:

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);
    }
}
我已经附加了颜色变化方法和破坏方法与预制。以下提供了相关脚本:-

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

    }
}
public class ball\u控制器:单行为{
[序列化字段]
公共int colorInt;
刚体2d rb;
//用于初始化
无效开始(){
//rb=this.gameObject;
colorInt=随机范围(1,5);
开关(colorInt){
案例1:
gameObject.GetComponent().material.color=color.red;
打破
案例2:
gameObject.GetComponent().material.color=color.green;
打破
案例3:
gameObject.GetComponent().material.color=color.blue;
打破
案例4:
gameObject.GetComponent().material.color=color.yellow;
打破
}
rb=this.gameObject.GetComponent();
rb.velocity=新矢量3(0f,-5f,0f);
}
//每帧调用一次更新
无效更新(){
}
无效OnTriggerEnter2D(碰撞R2D命中){
if(hit.gameObject.GetComponent().material.color==gameObject.GetComponent().material.color){
摧毁(这个游戏对象);
}否则{
销毁对象(this.gameObject);
}
}
}
但当它破坏游戏对象时。它给出了以下错误

MissingReferenceException:类型为“GameObject”的对象已被删除 已销毁,但您仍在尝试访问它。您的脚本应 请检查它是否为空,或者不应销毁该对象。“


它不再实例化球,并不断为它应该启动的每个球提供此错误。我希望我的脚本继续实例化克隆

我相信这可能是因为您正在用新实例化的ball覆盖
ball
。一旦球被破坏,对该组件的引用将被破坏。相反,您应该有两个独立的游戏对象变量-一个用于保存球预设,另一个用于保存脚本中对新球的引用:

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

我相信这可能是因为您正在用新实例化的ball覆盖
ball
。一旦球被破坏,对该组件的引用将被破坏。相反,您应该有两个独立的游戏对象变量-一个用于保存球预设,另一个用于保存脚本中对新球的引用:

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);
}
DestroyObject()做什么?DestroyObject()做什么?