- Home /
Examples on how to populate an array with prefabs
I am having trouble trying to add prefab objects into my array.
Here is the code I have so far:
using UnityEngine;
using System.Collections;
public class globalObjectArray : MonoBehaviour {
public GameObject[] myArray;
// Use this for initialization
void Start () {
myArray = new GameObject[30];
//add values
myArray[0] = ???;
}
}
and my prefabs are kept in Assets/Prefabs/
Even just examples of arrays containing prefabs would be awesome to look at.
Thanks!
Answer by xortrox · May 11, 2014 at 02:40 PM
You might wanna look into Resources.LoadAll When I use it I usually name my prefabs with a prefix like
Assets/Resources/Prefabs/Weapons/(0)SomeWeapon
Assets/Resources/Prefabs/Weapons/(1)SomeOtherWeapon
The parenthesis with a number in it is to ensure that Resources.LoadAll loads the prefabs into my array in that order
Resources class uses any folder named "Resources" inside your Assets folder, it can be a sub folder as well like "Assets/Data/Resources"
Then "bind" them to an enumeration with index values like this
public enum WeaponTypes
{
SomeWeapon = 0,
SomeOtherWeapon = 1,
}
As for loading:
//I usually name this class "Base"
//It also contains all my prefabs or other resources
using System.Linq;//For array casting
public static GameObject[] PrefabsWeapons{get; private set};
void Start()
{
PrefabsWeapons = Resources.LoadAll("Prefabs/Weapons").Cast<GameObject>().ToArray();
//How to access each weapon:
Debug.Log(PrefabsWeapons[(int)WeaponTypes.SomeWeapon]);
}
You can simplify your line with:
PrefabsWeapons = Resources.LoadAll<GameObject>("Prefabs/Weapons");
The cast is in the generic and the array is actually not useful since the method returns an array
Thanks xortrox!
You just answered about 3 questions I had but didn't even ask.
One more thing - what do you think would be a good way to add labels(variables? tags?) to your objects so you can spawn groups of objects by their characteristics (say I wanted to find only objects that were orange, or only objects that are vegetables...)
Use an enum of those type with the containing possibilities.
Then have a method on the script to check if the defined type matches:
public enum ColorEnum{
Red, Blue, Green, Orange
}
public class Script:$$anonymous$$onoBehaviour{
public ColorEnum colorEnum;
public bool CheckForEnum(ColorEnum ce){
return colorEnum == ce;
}
}
I got the array to build, but how are enums used to sort what goes into the array?
I sort of understand the concept of enums (like an array/boolean almost) but I have no idea how to actually implement them. Should they live in a separate script that I apply to the prefabs or should they live in the same file as the array builder and apply the values to the prefabs in another way?
Here is my current iteration:
using UnityEngine;
using System.Collections;
public class spawn$$anonymous$$ultiObject : $$anonymous$$onoBehaviour {
private Vector3 startPosition;
private float newXPos = 0f;
public float moveSpeed = 1f; // same as 1.0;
public float moveDistance = 4f;
public float timeLeftUntilSpawn = 0f;
public float startTime = 0f;
public float secondsBetweenSpawn = 1f;
public static GameObject[] myObjects;
void Start()
{
myObjects = Resources.LoadAll<GameObject>("Prefabs");
//idk below
//Debug.Log(myObjects[(int)objectList.???]);
startPosition = transform.position;
}
void SpawnRandomObject() {
//spawns item in array between position number 0 thru 49
int whichItem = Random.Range (0, 49);
//replace gameObjectSet with script that gathers preefabs from folder
GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
myObj.transform.position = transform.position;
}
// Update is called once per frame
void Update () {
newXPos = $$anonymous$$athf.PingPong (Time.time * moveSpeed, moveDistance) - (moveDistance /2f);
transform.position = new Vector3 (newXPos, startPosition.y, startPosition.z);
timeLeftUntilSpawn = Time.time - startTime;
if (timeLeftUntilSpawn >= secondsBetweenSpawn) {
startTime = Time.time;
timeLeftUntilSpawn = 0;
//Debug.Log ("Spawn Object");
SpawnRandomObject();
}
}
}
Your answer

Follow this Question
Related Questions
How to move prefabs array in sine wave? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[SOLVED] Problem with "foreach". 1 Answer
Unity event calling function gets nullreferencexception on bool 0 Answers