Spawn Prefab at one of 5 location cubes

Hey guys, I have a issue.

Im making a game where the escape pod is the ending and I have 5 cubes on the map placed around, I need it so one of those 5 cubes will spawn a prefab which will be the ending, so for now a cube. And every time the scene resets or loads again then it exit will change to another random cube which was placed on the game, so the ending is random well it varys from 1 of 5 locations.

here is a script I have which makes a object spawn 1 of 5 prefabs but it causes issue of making it so more then 1 cube will have the ending pod.

hope this makes séance :slight_smile:

 #pragma strict
 
 var hasWeapon : boolean = false;
 var weaponNumber : int;
 
 var weaponSpawner : Transform;        // Place where weapons spawn

 
 var Exit1 : GameObject;     // Weapon type = 1
 var Exit2 : GameObject;        // Weapon type = 2
 var Exit3: GameObject;        // Weapon type = 3
 var Exit4 : GameObject;        // Weapon type = 4
 var Exit5 : GameObject;        // Weapon type = 5
  
 function Start()
 {
     weaponNumber = Random.Range(1,5);
     SpawnWeapon();
 }
 
 
 function SpawnWeapon ()
 {
     if(weaponNumber == 1)
     {
         Instantiate(Exit1, weaponSpawner.position, weaponSpawner.rotation);
         hasWeapon = true;
     }
     
     else
     
    if(weaponNumber == 2)
     {
         Instantiate(Exit2, weaponSpawner.position, weaponSpawner.rotation);
         hasWeapon = true;
     }
     
     else
     
     if(weaponNumber == 3)
     {
         Instantiate(Exit3, weaponSpawner.position, weaponSpawner.rotation);
         hasWeapon = true;
     }
     
     else
     
     if(weaponNumber == 4)
     {
         Instantiate(Exit4, weaponSpawner.position, weaponSpawner.rotation);
         hasWeapon = true;
    }
    
         else
     
     if(weaponNumber == 5)
     {
         Instantiate(Exit5, weaponSpawner.position, weaponSpawner.rotation);
         hasWeapon = true;
    }
 }

Random.Range will not include the 5 in the roll so you’ll need to make that weaponNumber = Random.Range(1, 6);

Also, if I’m reading that right, I’m guessing more than 1 cube is beining instantiated?

If so, to safe guard, put this line at the start of function Start();:

if (hasWeapon) return;

Just so you can guarantee that this won’t be called more than once. I’d debug just to make sure that you are not calling this function more than once too.