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!
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?
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)]);
}
}