0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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…) \$\endgroup\$
    – Savlon
    Commented Nov 17, 2014 at 13:04
  • \$\begingroup\$ 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. \$\endgroup\$
    – rutter
    Commented Nov 17, 2014 at 22:43
  • \$\begingroup\$ Could you provide me with a code ? So i get a better understanding ? \$\endgroup\$
    – JekasG
    Commented Nov 18, 2014 at 1:38

1 Answer 1

2
\$\begingroup\$

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++)
{
    cubes.Add((GameObject)Instantiate(MyCubePrefab));
}

// 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

\$\endgroup\$
4
  • \$\begingroup\$ 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 ? \$\endgroup\$
    – JekasG
    Commented Nov 17, 2014 at 22:17
  • 1
    \$\begingroup\$ 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[] \$\endgroup\$
    – McAden
    Commented Nov 17, 2014 at 23:09
  • \$\begingroup\$ So how would i change the components from that GameObject Array ? \$\endgroup\$
    – JekasG
    Commented Nov 18, 2014 at 1:37
  • \$\begingroup\$ The same exact way you'd change the components on any other GameObject. \$\endgroup\$
    – McAden
    Commented Nov 18, 2014 at 5:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .