- Home /
Question by
Bluestrike · Feb 12, 2014 at 01:32 PM ·
for loopnull referance exeption
Pragma downcast on getcomponentsinchildren
Hi, The first code requires pragma downcast (but works ingame), the second code does not give compiler warnings but throws a null reference error. I rather don't use pragma downcast but can;t figure out why the 2nd code does not find the rigidbody
// requires pragma downcast
var RigidBodyArray :Component[];
RigidBodyArray = gameObject.GetComponentsInChildren(Rigidbody);
for(var rbody :Rigidbody in RigidBodyArray)
{
DoStuff();
}
// Causes null references in game
var RigidBodyArray :Rigidbody[];
RigidBodyArray = gameObject.GetComponentsInChildren(Rigidbody) as Rigidbody[];
for(var rbody :Rigidbody in RigidBodyArray)
{
DoStuff();
}
Comment
is Pragma downcast just mean #Pragma Strict. just curious
Pragma downcast hides downcast warnings when using pragma strict in a script.
Best Answer
Answer by KiraSensei · Feb 12, 2014 at 03:46 PM
Did you try :
var RigidBodyArray :Component[];
RigidBodyArray = GameObject.GetComponentsInChildren(Rigidbody);
for(var currComp :Component in RigidBodyArray)
{
var rBody:RigidBody = currComp as RigidBody;
DoStuff();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Use The Force Luke 1 Answer
What is the order of execution in a for loop? 1 Answer
Instantiate only one Prefab per 40 unit 2 Answers
Logic Problems using for loops 0 Answers
How to pass objects through array? 1 Answer