I am trying to make bombs in a grid-like manner. To do so, I have created nine empty objects called “Place 0,” “Place 1,” etc. until place 8 and ordered them into a 3x3 grid. One bomb spawns every ten seconds until the 3x3 grid is filled. How can I have bombs spawn in a random order and in such a way that two don’t spawn in the same place twice? I tried GameObject.Find(“Place “ + Random.Range(0, 8) and then, when a bomb spawned at a place, I changed the place’s name, but this ended with a nullreferenceexception that I could not figure out how to fix. Thanks!
Hey all, looks like I got it!
I’ve definitely tried this before, so I must have typed something wrong. Whatever.
Here’s the solution:
void Spawn()
{
GameObject spawnLoc = GameObject.Find("Place " + Random.Range(0, 8));
while (spawnLoc == null)
{
spawnLoc = GameObject.Find("Place " + Random.Range(0, 8));
}
Object.Instantiate(bombPrefab, spawnLoc.transform);
spawnLoc.name = "Placetaken " + bombCount; //bombCount is the number of bombs and is declared at the top of the script; if there is one bomb, then its place is named "Placetaken 0"
spawnTimer = 0;
bombCount++;
}