Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

picking a random prefab to instantiate?

Discussion in 'Scripting' started by darksblood, Apr 29, 2012.

  1. darksblood

    darksblood

    Joined:
    Apr 12, 2012
    Posts:
    31
    Hello there, I wanted to ask for some help from the community. I'm a bit new to programming (C#) and I'm making some small games to practice. On my current project, i made an empty game object that I'm refering to as "Spawner." I attached 3 different prefabs unto this gameobject. Now I'm wondering how i can make this spawner pick one of the three prefabs and instantiate it.

    I started here and i feel frozen now.

    public class Prefabs : MonoBehaviour
    {
    public GameObject Prefab1;
    public GameObject Prefab2;
    public GameObject Prefab3;
    {
    Anyone can help point me in the right direction? Thank you in advanced!
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    Try this.

    Code (csharp):
    1.  
    2. class Prefabs : MonoBehaviour
    3. {
    4.     List<GameObject> prefabList = new List<GameObject>();
    5.     public GameObject Prefab1;
    6.     public GameObject Prefab2;
    7.     public GameObject Prefab3;
    8.  
    9.     void Start()
    10.     {
    11.         prefabList.Add(Prefab1);
    12.         prefabList.Add(Prefab2);
    13.         prefabList.Add(Prefab3);
    14.        
    15.         int prefabIndex = UnityEngine.Random.Range(0,3);
    16.         Instantiate(prefabList[prefabIndex]);
    17.  
    18.     }
    19. }
    20.  
    More info.

    Good luck!
     
  3. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Just one optimization: You could even make the list public, add the GameObject prefabs to it directly and select a random prefab index like this:

    Code (csharp):
    1.  
    2. int prefabIndex = UnityEngine.Random.Range(0,prefabList.Count-1);
    3.  
    Would save you some lines of code and enable you to add as much prefabs to the list as you like withough having to assign them to specific variables.
     
    A_Marraff and Deleted User like this.
  4. darksblood

    darksblood

    Joined:
    Apr 12, 2012
    Posts:
    31
    TY so much. Really fast replies too, way faster than i had expected. Now i can continue. I really like that approach there. The more i practice, i understand the concepts more and get better. TY.
     
  5. diddydale_95

    diddydale_95

    Joined:
    Feb 12, 2014
    Posts:
    2
    When I applied this to my code, the List was changed to an IList and then I received the following error "The non-generic type `System.Collections.IList' cannot be used with the type arguments". Any help as to what's going on?
     
  6. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Last edited: Mar 10, 2014
  7. sago361

    sago361

    Joined:
    Jul 25, 2014
    Posts:
    1
    hello guys pls convert in into JavaScript Thanks :D
     
  8. tklplant

    tklplant

    Joined:
    Dec 6, 2013
    Posts:
    5
    how do i instantiate it from a moving position not a random
     
  9. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    This has already been answered, but since I have nothing better to do...
    You could also try this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class getMyPrefab : MonoBehaviour {
    5.     public GameObject[] prefabs = new GameObject[1]; //TODO: Replace '1' with total # of prefabs, don't forget to name you prefabs like "Prefab0 - Prefab9" etc.
    6.  
    7.     void Start(){
    8.         for(int p = 0; p < prefabs.Length; p++){
    9.             prefabs[p] = Resources.Load("Prefabs/Prefab" + p) as GameObject;
    10.         }
    11.         Instantiate(prefabs[Random.Range(0, prefabs.Length)]);
    12.     }
    13. }
     
    tklplant likes this.
  10. tklplant

    tklplant

    Joined:
    Dec 6, 2013
    Posts:
    5
    you are my savior thank you where has this been answered before ?
     
  11. tklplant

    tklplant

    Joined:
    Dec 6, 2013
    Posts:
    5
    wait sorry i think you misinterpreted my question i meant how do i get this
    1. class Prefabs : MonoBehaviour
    2. {
    3. List<GameObject> prefabList = new List<GameObject>();
    4. public GameObject Prefab1;
    5. public GameObject Prefab2;
    6. public GameObject Prefab3;

    7. void Start()
    8. {
    9. prefabList.Add(Prefab1);
    10. prefabList.Add(Prefab2);
    11. prefabList.Add(Prefab3);
    12. int prefabIndex = UnityEngine.Random.Range(0,3);
    13. Instantiate(prefabList[prefabIndex]);

    14. }
    15. }
    16. to not spawn within a area at a random position i want it to spawn on a objects transform please help