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