Storing an array's elements inside a prefab

I’m just wondering if it is possible for my array to remember the elements I have set within it. To expand on my issue, the array has 4 elements, all of which are GameObjects and have been predefined prior to being placed into a prefab. Unfortunately, when I use this prefab again (in the same scene) the elements need reassigning which isn’t good for what I need.

I understand why it does this. It’s because prefabs are seen more as blueprints so of course it won’t remember them, I wouldn’t expect a blueprint of a house to come with a real kitchen. I’m just wondering if there is anyway I can allow it to keep the assigned GameObjects or assign them on instantiation. Any help will be highly appreciated :slight_smile:

If you make the gameobjects children of the prefab. You would be adding them to the prefab. The prefab will only maintain connections that are part of its own structure. If you want to grab the connections you would have to find those objects through a script.

You can have links within the prefab, but you cannot save links to external objects. So if the object in the array are children, then it will work. So at runtime in order to establish the links to external game objects, you need to have some unique property of the set of object you assign to the array. You could find them by name:

var objects : GameObject[];

function Start() {
	objects = new GameObject[4];
	objects[0] = GameObject.Find("Blue");
	objects[1] = GameObject.Find("Green");
	objects[2] = GameObject.Find("Yellow");
	objects[3] = GameObject.Find("Red");
}

A more common approach is to tag all of your objects for the array the same and find them by tag.

var objects : GameObject[];

function Start() {
	objects = GameObject.FindGameObjectsWithTag("TheTag");
}

Depending on your need, there are other things you can do. Find all game object that contain a specific component/script for example, or find all game objects that are within a certain distance. But the concept is the same. You figure out something unique about the set of objects and then you build your array at runtime.