# How can I edit the components of an instantiated prefab?

Is it possible to edit one or more components of an instantiated prefab ?

For example if you instantiated five cube prefabs. Is it possible to reference each of the cube prefab and change a value individually ? Such as changing its position or even its scale.

If possible how can it be done ? Another request is, if this can be done. Could it be in the Update() function so when the game is running values can be experimented with.

• Yes it is possible… but are you looking to make them change randomly over time? Or if a certain Input is detected? (mouse click etc…) – Savlon Nov 17 '14 at 13:04
• If you're spawning the prefab using a script, the Instantiate call returns a reference to the newly spawned object -- after that, you can add, remove, or edit components just like you would with any other object. – rutter Nov 17 '14 at 22:43
• Could you provide me with a code ? So i get a better understanding ? – JekasG Nov 18 '14 at 1:38

Yes, unless you click "Apply" in the editor any changes made to a prefab instance are considered as overriding the prefab and do not affect either the prefab or the other instances of the prefab.

Once you instantiate the prefab you can change all aspects of that instance at your discretion. You do it the same exact way you manipulate any other object.

EDIT: To address the specific example you provided.

List<GameObject> cubes = new List<GameObject>();
// Instantiate 5 cubes.
for (int i = 0; i < 5; i++)
{
}

// Place them in a row, each one bigger than the last.
for (int i = 0; i < cubes.Count; i++)
{
float newScale = 0.5f * i;
float newX = 10 * i;
cubes[i].transform.position = new Vector3(newX, 0, 0);
cubes[i].transform.localScale = new Vector3(newScale, newScale, newScale);
}


All of the above could've (should've) been performed within the same loop but in this example I wanted to stress that each instance is completely separate and manipulating them isn't tied to instantiating them. Note that none of these changes I've made here are carried across to the either the prefab or the other instances of the prefab

• I want to edit them by script when they are instantiated. How can i keep reference to each one ? Do i keep them in reference of an array ? – JekasG Nov 17 '14 at 22:17
• Instantiate returns a reference to the instance, though you'll need to cast it GameObject myInstance = (GameObject)Instantiate(...); You can then store them separately or in a GameObject[] – McAden Nov 17 '14 at 23:09
• So how would i change the components from that GameObject Array ? – JekasG Nov 18 '14 at 1:37
• The same exact way you'd change the components on any other GameObject. – McAden Nov 18 '14 at 5:02