Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Spawn object on random location

Discussion in 'Scripting' started by undyingwill, Mar 30, 2017.

  1. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Hello, im making a game which is 98% percent finished, only lefts to randomize some objects.
    I have been searching how to make script for what i need for more than a month, but nothing..

    What i need is - lets say i have 10 objects. I have ~40 empty objects at specific locations around the world.
    These 10 objects must spawn random inside these 40 empty objects locations and they must me 1 at a location, no more.

    40 locations = 10 spawn
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,466
    Store all the spawn points in a list.

    Spawn the object at a random spawn point in the list and then remove the spawn point from the list to ensure that an object cannot be spawned there again.
     
  3. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Yeah, that is what I said, but just looks shorter.
    I have tried tutorials and scripts out there, but none of them works in unity5. I know that the script should be no more than 10 rows, but I may miss something, because nothing works..
     
  4. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    282
    So what does your script look like right now?
     
  5. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Code (JavaScript):
    1. var prefab : GameObject;
    2. var spawn1 : GameObject;
    3. var spawn2 : GameObject;
    4. var spawn3 : GameObject;
    5. public GameObject[spawn1, spawn2, spawn3] spawnPoints;
    6. public int randomNum;
    7.   function Start ()
    8.      
    9.       randomNum = Random.Range(0,3);
    10.       {
    11.      //SpawnerID = Random.Range(1, 3);
    12.      
    13.      if(spawn1){
    14.      
    15.      transform.position = spawnPoints[randomNum].transform.position;
    16.      
    17.      }
    18.      
    19.      if(spawn2){
    20.      
    21.      transform.position = spawnPoints[randomNum].transform.position;
    22.      
    23.      }
    24.      
    25.      if(spawn3){
    26.      
    27.      transform.position = spawnPoints[randomNum].transform.position;
    28.      
    29.      }
    30.      }
     
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,466
    Well, you're mixing both Javascript and C# in the same script file. You need to pick one and go with it.

    I would personally suggest C# because of many reasons.

    What you're trying to do with the spawning would require a List and I'm not so sure if Lists are available in JS.
     
  7. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Ok, what I have to add or delete from script ti make it work? I get some errors too, but is maybe because there is much languages inside script.

    Is there someone, who has already made similar script or a tutorial, so I can see how to create this script properly? There is some tutorials in youtube about that, but they are only from random spawn inside box collider, not specific.
    Think slender papers do the same, but there is no tutorials out there for them.
     
  8. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,466
  9. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Ok, i found script that does what i want ( from slender guide ), but it just clone my items (not random spawn item i've added, but random spawn a clone of a item). So i find the item clones at random places, but im required to find the real ones too or is like i found nothing. You can try if you want, create 5-6 cubes, 10-15 spawn places and you will see that the real cubes stays, and clones (useless if real stays)

    Code (JavaScript):
    1. #pragma strict
    2. import System.Collections.Generic; // to use List
    3.  
    4. public var spawnPoints : Transform[];
    5. public var paperPrefabs : Transform[];
    6.  
    7. private var availableSpawnPointsList : List.< Transform >;
    8. private var spawnList : List.< Transform >;
    9.  
    10.  
    11. function Start()
    12. {
    13.      ChooseRandomSpawnPoints();
    14.      SpawnPapers();
    15. }
    16.  
    17.  
    18. function ChooseRandomSpawnPoints()
    19. {
    20.      var i : int = 0;
    21.    
    22.      availableSpawnPointsList = new List.< Transform >();
    23.      spawnList = new List.< Transform >();
    24.    
    25.      // add all spawn points to a list
    26.      for ( i = 0; i < spawnPoints.Length; i ++ )
    27.      {
    28.          availableSpawnPointsList.Add( spawnPoints[i] );
    29.      }
    30.    
    31.      // choose a different random position for each paper prefab
    32.      for ( i = 0; i < paperPrefabs.Length; i ++ )
    33.      {
    34.          var rndPos : int = Random.Range( 0, availableSpawnPointsList.Count );
    35.        
    36.          spawnList.Add( availableSpawnPointsList[rndPos] ); // add this to the spawn point list
    37.        
    38.          availableSpawnPointsList.RemoveAt( rndPos ); // remove this from the available points list
    39.      }
    40.    
    41. }
    42.  
    43.  
    44. function SpawnPapers()
    45. {
    46.      // spawn paper at each location
    47.      for ( var i : int = 0; i < paperPrefabs.Length; i ++ )
    48.      {
    49.          var paper : Transform = Instantiate( paperPrefabs[i], spawnList[i].position, spawnList[i].rotation );paper.name = "Paper";
    50.          paper.name = "Paper";
    51.      }
    52. }