[SOLVED] How do I set the parent of an instance of a prefab that was placed into gameworld, not instantiated in a script?
I'm trying to set the parent of a gameobject that I have placed in the world. The gameobject is an instance of a prefab. When I run the code, it throws the error
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform)
This is the code snippit in question. It is inside a script attached to the prefab of the object I'm trying to change the parent of.
...
public GameObject handGrabContainer;
...
public void beingGrabbed(){
...
this.transform.parent = handGrabContainer.transform;
...
}
I have searched online for a solution to this error, and there are several threads about it here, here and here for example. However, in all of those examples, the object is being instantiated in the script. Like this
GameObject myThing= Instantiate(thing, Vector3 (0, 0, 0)) as GameObject;
myThing.transform.parent = transform;
But I have an object that was placed into the gameworld in the Unity editor. How can I access the transform of the actual object, instead of the prefab, if I am not creating the object a script?
EDIT: It seems that problem was actually with the "handGrabContainer" object. I had it linked to the script in the script's prefab. The error that I was getting was from the handGrabContainer, not the this.transform.
Answer by jmonasterio · Dec 11, 2015 at 11:56 PM
I'd suggest: Put a breakpoint on the bad line. And stop there in Visual Studio debugger.
Hover over the "this" (or add it to Watch window) in VisualStudio. Take a look at it. I bet "this" is not what you think it is.
Hello, thanks for your response. I did what you said, but I used $$anonymous$$onoDevelop ins$$anonymous$$d. It appears that "this" is an instance of the prefab. Alright, so what do I have to do to access the specific object, not the prefab?
Where is the code where you are calling beingGrabbed()? There you've got some object, and perhaps that's where to look.
You may be calling beingGrabbed on your Prefab, ins$$anonymous$$d of an object you Instantiated(). Hard to tell without seeing code.
As I said, I'm not using "Instantiate()" to create the object. I'm placing it in the world in the Unity Editor. The script is on the prefab of the object.
Answer by monfera · Aug 14, 2016 at 11:54 PM
Just supply the fourth argument for Instantiate
, which is the parent
. No need for an extra line for this.
Your answer
Follow this Question
Related Questions
how add child to multi gameobject 0 Answers
Following object (arrow) slides off of a object 1 Answer
Parent-Child axial reference problem 0 Answers
Use Raycast to select empty parent game object? 1 Answer
How to move an object left and righ (looping) when it still move to left with it parent 0 Answers