Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Programmatically created prefab clones don't update

Discussion in 'Prefabs' started by nowletsstart, Feb 7, 2019.

  1. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    I used the code below to programmatically create clones of my prefabs. When I try to resize the prefab in prefab mode by none of the clones are updated. I used a simple sprite image to create my prefab and added it through the inspector using a public GameObject property.

    Code (CSharp):
    1.  for (int i = 0; i < tar.numOfItems; i++)
    2.             {
    3.                 var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
    4.                 var localPos = rot * Vector3.right * tar.radius;
    5.                 tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
    6.                 tar.transform.position + localPos, rot));
    7.                 angle += angleBetween;
    8.                 tar.spawnedObjects[i].name = tar.spawnedObjects[i].name + (i + 1);
    9.              
    10.             }
    So the clonedObject is a public GameObject field added through the inspector. Am I creating my prefab clones in the right way, before I am having issues updating them?
    Please note that I am using Unity 2019.1.
     
    Last edited: Feb 7, 2019
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  3. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The same way as with Instantiate, you just have to set the position and rotation afterwards.
     
    nowletsstart likes this.
  5. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    Thanks a lot, can't believe that didn't occur to me.