预制件不像它的起源一样工作

本文关键字:工作 一样 起源 | 更新日期: 2023-09-27 18:11:47

public class green : MonoBehaviour
{    
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "BLUE")
        {
            Destroy(other.gameObject);
            gm.mylife -= 1;
        }
    }
}
public class gm : MonoBehaviour
{    
    public GameObject blue;
    static public bool tr = false;
    public Text life;
    public static int mylife = 0;
    void Start()
    {
        makebox();      
    }
   void makebox()
   {
        StartCoroutine("timedelay");
   }             
   IEnumerator timedelay()
   {
       yield return new WaitForSeconds(3f);
       Debug.Log("sdfaDF");
       GameObject br = Instantiate(blue, new Vector3(-6, -2, 0), Quaternion.identity) as GameObject;
       makebox();
   }
   void Update()
   {
       life.text = (mylife.ToString());
   }
}

我创造了一个蓝色的盒子,当它遇到某些东西时它会被摧毁,并且得分为-1。它在(-2,2)的位置。然后我做了一个预制件。但预制件并不是它的起源。它只是在原点相同的位置创建。我想让我的预制物毁灭并获得-1分。我怎样才能修好它?

预制件不像它的起源一样工作

你一直在同一位置生成对象:

GameObject br = Instantiate(blue, new Vector3(-6, -2, 0), Quaternion.identity) as GameObject;

相反,你应该像这样每次给出一个新的位置:

public Vector3 offSet = new Vector3(2,0,0);
Vector3 pos;
IEnumerator timedelay()
{
    yield return new WaitForSeconds(3f);
    Debug.Log("sdfaDF");
    GameObject br = Instantiate(blue, pos, Quaternion.identity) as GameObject;
    pos += offSet;
    makebox();
}

GameObject blue = Instantiate(bluep);蓝色的。标签= "BLUE";

我通过在脚本中给对象添加标签来解决。