- Home /
Recursive Rigidbody Method
,Hi guys. Wrote a method which find highest gameObject parent with Rigidbody.
public Rigidbody RecursiveReturnRigidbodyParent(GameObject gameObjectGrabed)
{
while (gameObjectGrabed.GetComponentInParent<Rigidbody>())
{
gameObjectGrabed = gameObjectGrabed.transform.parent.gameObject;
}
return gameObjectGrabed.GetComponent<Rigidbody>();
}
Aaand thats the problem:
NullReferenceException: Object reference not set to an instance of an object
Im grabbing object using raycast. If the object contains rigidbody then it search further if any of the parent object have rigidbody.
Answer by Cornelis-de-Jager · Mar 25, 2019 at 09:51 PM
Because you are getting a GameObject that might have no rigidbody and you are returning that.
public Rigidbody RecursiveReturnRigidbodyParent(GameObject gameObjectGrabed)
{
// Ensure the current object has a rigidBody
if(gameObjectGrabed.GetComponentInParent<Rigidbody>() == null)
return null;
var currObject = gameObjectGrabed.GetComponentInParent<Rigidbody>() ;
while (gameObjectGrabed.GetComponentInParent<Rigidbody>() != null)
{
currObject = gameObjectGrabed.GetComponentInParent<Rigidbody>();
gameObjectGrabed = gameObjectGrabed.transform.parent.gameObject;
}
return currObject;
}
Thanks Sir but thats still the problem, the same as before. Is it working for you ?
Your answer
Follow this Question
Related Questions
Pick Up Object using Rigidbody FPS 0 Answers
object Pass through the polySurface Maya imported scene. 1 Answer
Best way to throw object 0 Answers
[Solved]Object reference not set to instance of object in OnTriggerEnter 1 Answer
Turning an object into a RigidBody and applying a force on it... 2 Answers