- Home /
set a objects child to have no parrent?
I'm making a script where you can pick objects up and put them down. This is the part that puts it down.
here are parts of the script
var scriptHolder : GameObject;
//if a raycast hits a rigidbody
if(hit.rigidbody && Input.GetMouseButtonUp(1)){
//trying to get the child of scriptHolder and change its child to have no parrent
gameObject.GetComponents(scriptHolder).transform.parrent = null;
}
thanks in advance :)
you gave me the answer but how would i set the child of "var scriptHolder"
to use gravity.
any way, use gameObject.Find("child name").attachedRigidbody.useGravity = false/true;
Answer by s_guy · Oct 25, 2013 at 05:32 AM
It's not discernible what you're trying to do, but...
You can set any game object's transform to the root of the hierarchy by setting it to null. You can't do this to a component other than a transform without an error or unintended results. Most components other than the transform will not have a parent.
GetComponent() does not get a child. It gets a component. Components belong to a game object and generally do not have parents or children in any sense.
Game objects have components. One component of a game object is its transform. You can access this with .transform from the game object, and not from a component.
Transforms have parents and children. This is the structure you see in the Hierarchy.
Indeed, that is why I removed my answer still the purpose to have no parent but then to add gravity and finally to have no parent again...
You can set any game object's transform to the root of the hierarchy by setting it to null. This is the same as having no parent in most senses (same as a newly created object in the editor). Sorry that I wasn't clear.
I missed coolguy's comment, but you can add gravity to any game object independent of it being parented or not. One easy way to do this is to give it a rigidbody and that will use default gravity. Though, it creates problems if a parent has a rigidbody and one of its children (or grandchildren) does too.
I could give a better answer if I understood the intent.