L
L
LeiksGames2021-09-12 21:41:26
Unity
LeiksGames, 2021-09-12 21:41:26

How to spawn a unity2d object?

How to spawn in such a way that 1 of 3 objects would be selected and spawned after 1 - 2 seconds?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KraGen.Developer, 2021-10-25
@LeiksGames

public GameObject obj1;
public GameObject obj2;
public GameObject obj3;

public void Start(){
      StartCoroutine(Spawner());
}

IEnumerator spawner(){
     while(true){
      yield return new WaitForSecond(2f);
      int obj = Random.Range(1,3);
      switch(obj){
      case 1:
      Instantiate(obj1, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);//свои координаты лепите
      break;
      case 2:
      Instantiate(obj2, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);//свои координаты лепите
      break;
      case 3:
      Instantiate(obj3, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);//свои координаты лепите
      break;
}
}

M
MrSlowpoke, 2021-09-13
@MrSlowpoke

Coroutine for a random time between 1-2 seconds. Instantiate on "spawn" or object pool if for the sake of optimization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question