picking a random prefab to instantiate?

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!

Try this.

class Prefabs : MonoBehaviour
{
    List<GameObject> prefabList = new List<GameObject>();
    public GameObject Prefab1;
    public GameObject Prefab2;
    public GameObject Prefab3;

    void Start()
    {
        prefabList.Add(Prefab1);
        prefabList.Add(Prefab2);
        prefabList.Add(Prefab3);
        
        int prefabIndex = UnityEngine.Random.Range(0,3);
        Instantiate(prefabList[prefabIndex]);

    }
}

More info.

Good luck!

4 Likes

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:

int prefabIndex = UnityEngine.Random.Range(0,prefabList.Count-1);

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.

2 Likes

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.

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?

Hi,

You need to add this line at the top of you script :

using system.collections.generic;

You have a link here that can explain you how the List and Dictionary work:
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

hello guys pls convert in into JavaScript Thanks :smile:

how do i instantiate it from a moving position not a random

This has already been answered, but since I have nothing better to do…
You could also try this:

using UnityEngine;
using System.Collections;

public class getMyPrefab : MonoBehaviour {
    public GameObject[] prefabs = new GameObject[1]; //TODO: Replace '1' with total # of prefabs, don't forget to name you prefabs like "Prefab0 - Prefab9" etc.
  
    void Start(){
        for(int p = 0; p < prefabs.Length; p++){
            prefabs[p] = Resources.Load("Prefabs/Prefab" + p) as GameObject;
        }
        Instantiate(prefabs[Random.Range(0, prefabs.Length)]);
    }
}
1 Like

you are my savior thank you where has this been answered before ?

wait sorry i think you misinterpreted my question i meant how do i get this

  • class Prefabs : MonoBehaviour
  • {
  • List prefabList = new List();
  • public GameObject Prefab1;
  • public GameObject Prefab2;
  • public GameObject Prefab3;
  • {
  • prefabList.Add(Prefab1);
  • prefabList.Add(Prefab2);
  • prefabList.Add(Prefab3);
  • Instantiate(prefabList[prefabIndex]);
    • }
  • }
  • to not spawn within a area at a random position i want it to spawn on a objects transform please help