0
votes

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.

2
What do you mean by prefab mode? Do you change the size of the prefab in play mode? What is tar an arbitrary class instance?Ali Kanat
@AliKanat I was able to solve it not long ago. I just posted an answer. Prefab Mode is part of the new prefab workflow in Unity 2019.1, I wasn't using the prefab mode in play mode. The code I provided was in the editor class.Zizo

2 Answers

1
votes

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);

    }
0
votes

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