Can not access GetComponentInParent ( ). usegravity
Hi i have 2 gameobjects Parent and Child. Child has a script when OnCollisionEnter with a wall it will call GetComponentInParent < Rigidbody > ( ). usegravity and set it to true. Whenever i run it the game the gravity seems to switch in the child not in the parent what i am doing wrong ? all of the objects have Rigidbodies and Colliders.
Thanks.
//Script attached to the child.
void OnCollisionEnter(Collision other)
{
GetComponentInParent < Rigidbody > ( ). usegravity = true;
}
If i want to access a script in the parent then GetComponentInParent < Parent Script > ( ) ; works with no problem.
Answer by Dave-Carlile · Oct 15, 2015 at 02:48 PM
The documentation for GetComponentInParent states:
Returns the component of Type type in the GameObject or any of its parents.
So the function will look for the component in the current object first and only look in the parent if it's not found. I assume your child object has a Rigidbody. You can use transform.parent.GetComponent
to look just in the parent.
thanks! it was something I haven't even considered that it might start looking for instance at the object itself before moving to parents... what's the point of doing that is beyond me, name GetComponentInParent is very misleading
Your answer
Follow this Question
Related Questions
Moving grabbed object OnTriggerStay2D 0 Answers
Objects lose collision when set as child? 0 Answers
Instantiating as a child of an object in hierarchy 1 Answer
How do I spawn an array of prefab clones as children of an object? 1 Answer
How could I make the children of an object face the direction it is moving on the y-axis? 0 Answers