简体   繁体   English

如何在Unity3D(5.1)中调用重复并销毁对象

[英]How to Invokerepeat and destroy objects in Unity3D(5.1)

I'm trying to make a game with similar type of gameplay as crossyroads, like with infinity level generation/enemy generation. 我正在尝试制作一种具有与跨界相似的游戏方式的游戏,例如无限级生成/敌人生成。 Everything seems to work fine with the ground and enemy generation in my code, and it works when I play. 在我的代码中,一切似乎都可以与地面和敌人配合,并且在我玩游戏时也可以正常工作。 But I can't make the new generated enemies to repeat themselves. 但是我不能让新产生的敌人重演。 I've tried to put the "Invoke repeating function" in void Start, void Update and inside the for loop. 我试图将“调用重复功能”放在void Start,void Update和for循环中。 But it doesn't work. 但这是行不通的。 It only sends out one enemy. 它只会散发出一个敌人。

And then I have problem to refer/find the new created objects(plane/enemies) so I can destroy them when they are out of screen. 然后,我在引用/查找新创建的对象(飞机/敌人)时遇到问题,因此当它们超出屏幕时,我可以销毁它们。

I must have structured the code in a wrong way, but I can't figure it out. 我必须以错误的方式构造代码,但是我无法弄清楚。

    public class levelGenerationScrip : MonoBehaviour {

public GameObject greenPlane;
public GameObject bluePlane;
public GameObject brownPlane;
public GameObject testEnemy;

int firstRand;
int secondRand;
int distPlayer = 12 ;
int enemyDist= 7;

Vector3 intPos = new Vector3 (0,0,0);
Vector3 enemyPos = new Vector3 (0,0,0);
Vector3 enemyPos2 = new Vector3 (0,0,0);

void Start () {

}

void Update () {

    if (Input.GetKeyDown("w"))
    {
        firstRand = Random.Range(1,4);
        if(firstRand == 1)
        {
            secondRand = Random.Range(1,1);
            for(int i = 0; i < secondRand; i++)
            {
                intPos = new Vector3(-2.3f,0.5f,distPlayer);
                enemyPos = new Vector3 (7,1.51f,enemyDist);

                distPlayer += 1 ;
                enemyDist += 1;

                GameObject greenIns = Instantiate(greenPlane) as GameObject;
                greenIns.transform.position = intPos ;

                GameObject testEn = Instantiate (testEnemy) as GameObject;
                testEn.transform.position = enemyPos ;
                testEn.GetComponent<Rigidbody> ().AddForce(new Vector3 (-350, 0, 0));
                //InvokeRepeating ("testEn",1,1.5f);
            }

        }
        if(firstRand == 2)
        {
            secondRand = Random.Range(1,1);
            for(int i = 0; i < secondRand; i++)
            {
                intPos = new Vector3(-2.3f,0.5f,distPlayer);
                enemyPos2 = new Vector3 (-11,1.51f,enemyDist);
                distPlayer += 1 ;
                enemyDist += 1;

                GameObject blueIns = Instantiate(bluePlane) as GameObject;
                blueIns.transform.position = intPos ;

                GameObject testEn = Instantiate (testEnemy) as GameObject;
                testEn.transform.position = enemyPos2 ;
                testEn.GetComponent<Rigidbody> ().AddForce(new Vector3 (+350, 0, 0));
            }
        }
        if(firstRand == 3)
        {
            secondRand = Random.Range(1,1);
            for(int i = 0; i < secondRand; i++)
            {
                intPos = new Vector3(-2.3f,0.5f,distPlayer);
                enemyPos = new Vector3 (7,1.51f,enemyDist);

                distPlayer += 1 ;
                enemyDist += 1;


                GameObject brownIns = Instantiate(brownPlane) as GameObject;
                brownIns.transform.position = intPos ;

                GameObject testEn = Instantiate (testEnemy) as GameObject;
                testEn.transform.position = enemyPos2 ;
                testEn.GetComponent<Rigidbody> ().AddForce(new Vector3 (+400, 0, 0));
            }
        }

    }

}

} }

By "Unity" in your tag i take it you meant to put "Unity3d" 在您的代码中使用“ Unity”,我认为您打算放置“ Unity3d”

After instantiation of your gameobjects, shove them into a global list variable, now you can refer to them anytime you so please. 实例化游戏对象后,将它们推到全局列表变量中,现在您可以随时参考它们。

// global var
List<GameObject> objs = new List<GameObject>();

// inside function
GameObject blueIns = Instantiate(bluePlane) as GameObject;
objs.Add(blueIns);

GameObject testEn = Instantiate (testEnemy) as GameObject;
objs.Add(testEn);

And as for your InvokeRepeating problem, you havent posted your usage of it so i cannot tell you what you are doing wrong. 至于您的InvokeRepeating问题,您还没有发布它的用法,所以我无法告诉您您在做什么错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM