预制件的起源不同

时间:2016-10-05 09:16:55

标签: c# unity3d

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)位置制作的。 然后我做了一个预制件。但预制件不起作用。 JUST创建在与其原点相同的位置。 我想让我的预制件破坏并得分-1。 我该如何解决? 请帮助我...

2 个答案:

答案 0 :(得分:0)

您始终在同一位置产生对象:

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

答案 1 :(得分:0)

GameObject blue = Instantiate(bluep);  blue.tag =" BLUE&#34 ;;

我通过在脚本中为对象提供标记来解决。