- Home /
Getting an error every time I try and access an Instantiated objects methods.
Rigidbody newBall = Instantiate(ball, position, transform.rotation) as Rigidbody;
newBall.transform.localScale = new Vector(.5f,.5f,.5f)
Returns and null reference error.
I'd personnally avoid writing float without the integer number. It leads to confusion and won't compile in a declaration. var myFloat:float = .5f; won't work
@BY0LOG1C: Huh? "var myFloat:float = .5f;" is perfectly valid, although it's JS and not C#. In C#, "var myFloat = .5f;" or "float myFloat = .5f;" are fine.
The answer to the problem should be:
GameObject newBall = Instantiate(ball, position, transform.rotation) as GameObject;
newBall.transform.localScale = new Vector(.5f,.5f,.5f)
Totally my bad, must've tested it too quickly... You always pick up my errors, Eric! Sorry for the erroneous info but in any case, I'd still write floats as the full number 0.5f. Another two cents of $$anonymous$$e.
Answer by Eric5h5 · Apr 09, 2012 at 10:56 PM
I would assume that your "ball" prefab is not of type Rigidbody, so trying to cast newBall to Rigidbody results in null.
Sorry, misunderstood the question, assumed heretic was trying to add a rigidbody component. Deleted my wrongfuly accepted answer.
yeah, but your comment lead me to the right answer., not saying Eric5h5 was wrong, yours just got me there.
Now its marked as answered but there's no answer! I'd edited my post to reflect the new information (point to Eric's post). Well that's my two cents :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613070122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
instantiate a gameobject or rigidbody? 1 Answer
dont instantiate clones 1 Answer
Instantiating a Rigidbody instead of a GameObject 1 Answer
Instantiate prefab to the parent 2 Answers