Home > other >  Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corrup
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corrup

Time:11-24

I am facing this problem in Unity again and again. I cannot find solution. I think my code is right, but where is the problem?

GameObject visual = Instantiate<GameObject>(placementObject[num], hit.point   visualOffset, Quaternion.identity); 
visual.transform.SetParent(parentObject.transform); <--- Causing error

Any suggestions? Thank you for any feedback!

CodePudding user response:

you could set the parent directly:

GameObject visual = Instantiate<GameObject>(placementObject[num], hit.point   visualOffset, Quaternion.identity, parentObject.transform); 

but using SetParent after Instantiate() should work, too. Check whether parentObject is not null.

CodePudding user response:

You are trying to assign a parent that is a prefab and not an instance of an object in your scene.

Ensure that parentObject is an actual object and not a prefab (or, if you're editing a prefab, ensure that it is an actual object within that prefab, and not an unreferenced prefab itself).

  • Related