Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random scene elements?

Discussion in 'Scripting' started by Fillock, Jun 10, 2015.

  1. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    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.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.

    http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

    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)

    https://msdn.microsoft.com/en-us/library/5cw9x18z(v=vs.110).aspx

    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.
     
  3. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    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
     
  4. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    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.
     
  5. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Thanks for answering! I try out your ideas now but I'm a slow coder and need some more time to make it work.
     
  6. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    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.
     
  7. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    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?
     
  8. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    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:

    Code (csharp):
    1.  
    2. Public GameObject[] SpawnPoints; //Create an Array to place all of the Spawners
    3. Public GameObject CUP //Create a public GameObject to place our prefab
    4. Public int ItemAmount=5; //Set the amount we want to spawn
    5.  
    6. void Start() {
    7. SpawnPoints = GameObject.FindGameObjectsWithTag("Spawner"); //Find all GameObjects and place them in the array
    8. SpawnCup();
    9. //You will want to run this and the above either on Awake or at Start, not in an update
    10. //otherwise they'll constantly spawn and you'll get some lag. You can call SpawnCup whenever though
    11. //but it's best to set the SpawnPoints at the Start/Awake
    12. }
    13.  
    14. void SpawnCup() { //Call this Method to Spawn the Cups
    15. for(int i=0; i < ItemAmount; i++) { //Create a for loop to run for how man ItemAmount we specified
    16. Transform Spawn = SpawnPoints[Random.Range(0, SpawnPoints.Length -1)].transform; //Set a transform to equal a randomly chosen SpawnPoint
    17. Instantiate(CUP, Spawn.position, Spawn.rotation); //Instantiate the prefab at the chosen Spawner
    18. }
    19. }
    20.  
    21.  
    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.
     
    Last edited: Jun 10, 2015
  9. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Thanks :)

    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...
     
  10. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    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.
     
  11. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    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.
     
  12. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    What part of the code is getting that error? It could be because I was converting the Array to a transform try this instead:

    Code (csharp):
    1.  
    2. GameObect Spawn = SpawnPoints[Random.Range(0, SpawnPoints.Length-1)];
    3. Instantiate(CUP, Spawn.transform.position, Spawn.transform.rotation);
    4.  
     
  13. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    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"
     
  14. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    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.
     
  15. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73