Random scene elements?

Hi.

I want to pre-make lets say 15 scenes, and I want to manually place let say 30 (empty) objects on each scene. And then I need a script to random place a selection of objects on these 30 objects are. This way I can drive the same scene many times and still make it look different.

I know a little about the Random class and how to select objects by there Tag, but don’t know how to make them appear in predefined places, ides anyone?

Fillock.

give all the “spawnPoints” a tag and create a list for them using the FindGameObjectsWithTag in a start function, I assume the items to be placed will be setup manually in the inspector? in which case a list for them would be fine.

you can access a random spawn point using Random.Range and the list index myList[someRandomIndex], you can remove an element from the list and have it shrink by using RemoveAt(index)

you’ll want to use lists rather than arrays because of this shrinking behaviour, if you remove the spawnPoints as you use them, and also remove the possible objects as you place them you won’t duplicate an object or try to place two things on one object.

You could use only one scene if you make a prefab out of each manually placed objects group.

the way you can use random in this case could be:
Spawn an object group (could make each group by placing all your objects and then making an empty object on 0,0,0 as their parent)

Once you have the group you check on it’s children with something like

foreach( Transform child in yourObjectGroupParent.transform )
{
//here you iterate with the random
instantiate( objectlist[Random.Range(0, objectList.Count)], child.position, Quaternion.identity);
}

object list would be a list with all the small prefabs you want to randomize

You have two ways really, you could create a tile system and then randomly spawn an object on a random tile or do what was listed above. Place a tag (e.g. Spawner) on an empty object, place these where you’d like objects to spawn and in your script do:

gameobject = GameObject.FindGameObjectsWithTag(“Spawner”);

Then you have an Array of all of the spawn points, it’s then as easy as doing:

spawnPoint = gameobject[Random.Range(0, gameobject.Length -1)];

instantiate(prefab, spawnPoint.transform.position, spawnPoint.transform.rotation);

You could even make an array and spawn a random object this way too.

Thanks for answering! I try out your ideas now but I’m a slow coder and need some more time to make it work.

If you ever want or need a detailed explanation I’d be happy to help, but in all honesty the best way to learn is to just mess around with different ideas.

BlueInferno, I only make your code to generate 1 Instance, am I doing something wrong?

About randomness, if I lose my game and have to start all over again, then I actually can not re-visit the exactly same scene since it is partly generated randomly. Would it be an idea to use a txt file with pre-made random number? For example each time you drive on scene 5 it use next chunk with static premade numbers (for, lets say buildings), this way you can get killed and just reload last chunk with numbers when you enter scene 5 again… Is it a good idea or not?

How many spawn points do you have out? Make sure they all have the exact same tag. So say for example you wanted to spawn a cup say, 5 times? But each at random places and you had 50 spawn points with the same tag of Spawner. Then you could do this:

Public GameObject[] SpawnPoints; //Create an Array to place all of the Spawners
Public GameObject CUP //Create a public GameObject to place our prefab
Public int ItemAmount=5; //Set the amount we want to spawn

void Start() {
SpawnPoints = GameObject.FindGameObjectsWithTag("Spawner"); //Find all GameObjects and place them in the array
SpawnCup();
//You will want to run this and the above either on Awake or at Start, not in an update 
//otherwise they'll constantly spawn and you'll get some lag. You can call SpawnCup whenever though 
//but it's best to set the SpawnPoints at the Start/Awake
}

void SpawnCup() { //Call this Method to Spawn the Cups
for(int i=0; i < ItemAmount; i++) { //Create a for loop to run for how man ItemAmount we specified
Transform Spawn = SpawnPoints[Random.Range(0, SpawnPoints.Length -1)].transform; //Set a transform to equal a randomly chosen SpawnPoint
Instantiate(CUP, Spawn.position, Spawn.rotation); //Instantiate the prefab at the chosen Spawner
}
}

I haven’t tested this code, but it should work. If you get any errors be sure to let me know and I’ll do my best to explain. If you don’t understand anything I’ve written either feel free to ask.

Thanks :slight_smile:

It is say; “Syntax error; bad array decleration”.

I code in Javascript, but I think I understand most of your code, but I don’t know how to fix this…

Ah I’m sorry I code mostly in C# but it should be almost exactly the same as that, but the Array might be different. I’ll see if I can find any docs or tutorials and let you know.

You don’t need to help me make it in JS, but if you help me solve the Syntax in C# it would be nice.

What part of the code is getting that error? It could be because I was converting the Array to a transform try this instead:

GameObect Spawn = SpawnPoints[Random.Range(0, SpawnPoints.Length-1)];
Instantiate(CUP, Spawn.transform.position, Spawn.transform.rotation);

I try the newlines but it do not work yet I’m afraid.

it say: “ine 3,8 Syntax error… to declare a manage array the rank specifier precedes the variables identifier. To declare a fixed size buffer field, use the fixed keyword before the field type”

Weird that error is due to the array not having a set length, but you shouldn’t need to do that. Let me put the code into a new scene and see if I can recreate the error.

Hi!

I think I found what I was looking for at: Unity - Scripting API: GameObject.FindGameObjectsWithTag

I still has a lot to understand, but now I can spawn om dummy’s and that’s good enough to start testing on, thanks for all help :slight_smile: