Quantcast
Channel: Questions in topic: "invokerepeating"
Viewing all articles
Browse latest Browse all 220

Prevent object from spawning at a spawn point twice in a row?

$
0
0
I have a set of spawn points and an object that I want to spawn from a random spawn point every second. I've used the InvokeRepeating function to make this work. But now, I want to modify the code a little bit. I don't want the the object to be spawned from the same spawn point twice in a row. Let's say I have 5 spawn points: A, B, C, D, and E. When InvokeRepeating runs, the object is spawned randomly from spawn point C. So when InvokeRepeating runs again, I don't want the object to spawn at spawn point C. It has to randomly pick another spawn point. I don't want the object to spawn at at a particular spawn point twice in a row. Here my code: void Start () { GameObject[] SP = GameObject.FindGameObjectsWithTag ("spawnPoint"); spawnPoints = new Transform[SP.Length]; for(int i=0; i < SP.Length; i++) { spawnPoints[i] = SP[i].transform; } InvokeRepeating("SpawnObject", spawnTime, spawnTime); } void SpawnObject() { int spawnPointIndex = Random.Range(0,spawnPoints.Length); Instantiate(theObject, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation); } Any idea what I can add to my code so solve this?

Viewing all articles
Browse latest Browse all 220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>