Zizo
Zizo

Reputation: 31

Prefab clones made in script not updating

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.

or (int i = 0; i < tar.numOfItems; i++)
        {
            var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
            var localPos = rot * Vector3.right * tar.radius;
            tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
            tar.transform.position + localPos, rot));
            angle += angleBetween;
            tar.spawnedObjects[i].name = tar.spawnedObjects[i].name + (i + 1);

        }

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.

Upvotes: 0

Views: 450

Answers (2)

Stark355
Stark355

Reputation: 26

What's about using this one? tar.spawnedObjects[i].transform.SetParent(tar.ClonedObject);

Upvotes: 0

Zizo
Zizo

Reputation: 31

I was suppose to instantiate prefabs using PrefabUtiltity.InstantiatePrefab since I am using Unity 2019.1 but I was instantiating it as a usual game object style instantiation.

for (int i = 0; i < tar.numOfItems; i++)
    {
        var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
        var localPos = rot * Vector3.right * tar.radius;

        tar.spawnedObjects.Add(PrefabUtility.InstantiatePrefab(tar.clonedObject as GameObject) as GameObject);
            tar.spawnedObjects[i].transform.position = tar.transform.position + localPos;
            tar.spawnedObjects[i].transform.rotation = rot;

        angle += angleBetween;
        tar.spawnedObjects[i].name = tar.spawnedObjects[i].name + (i + 1);

    }

Upvotes: 1

Related Questions