Hi friends,
How do we declare a static prefab array???
Regards,
vanhouten777.
Hi friends,
How do we declare a static prefab array???
Regards,
vanhouten777.
Hi Friends,
Please reply.
Regards,
vanhouten777.
Declaring it is easy, filling it with meaningful data is the challenge!
In this situation I would use one of two options:
Resources.LoadAll. By putting the prefabs intended for your array in a folder, you can just load every prefab in the folder into an array.
A singleton, with the array being on the object. You can fill the array there.
public class ArrayHolder : MonoBehaviour {
public static ArrayHolder main;
void Awake() {
main = this;
}
public GameObject[] myPrefabs;
// you can now access this from anywhere with ArrayHolder.main.myPrefabs
}
might make more sense to use a list instead of a array for this
so just do the above and use
List<GameObject> myobjList = new List<GameObject>();
reason being that lists are dynamic in size, and objects can easily be added with the .Add(myObject) method.
Tough to say without knowing his use case, but I’m guessing the array is not going to be changed at runtime, in which case a builtin array is a better choice. A smidgeon more memory-efficient and better to loop through. (foreach over a List causes memory allocations, which contribute to garbage collector stutters)
HI Friends,
I have one prefab for which I want to make a array of that one prefab.
Regards,
vanhouten777.
wait, why would you want an array of one prefab?
You want an array of length 1?
I think he wants a bunch of copies of one prefab instantiated into the scene. Something like this:
public GameObject myPrefab;
public int count = 5;
public Vector3 offset = new Vector3(1f, 0f, 0f);
void Start() {
Vector3 currentPos = transform.position;
for (int o=0;o<count;o++) {
Instantiate(myPrefab, currentPos, Quaternion.identity);
currentPos += offset;
}
}