- Home /
Question by
TomMakesGames · May 23, 2013 at 11:01 PM ·
rigidbodyinstanceobject reference
Trying to assign new name, Unity says it can't find the object?
On the line where I'm trying to give a new name to a recently instantiated rigidbody, Unity is telling me that "Object reference is not set to an instance of an object".
I've checked a few more questions/answers and I thought I was declaring this variable properly before trying to use it.
Have I missed something? Many thanks for your help.
Rigidbody newplayerShot = Instantiate (playerShot, launchSite, transform.rotation)as Rigidbody;
newplayerShot.name = "playerShot";
newplayerShot.rigidbody.velocity = transform.up * shotSpeed;
Comment
Best Answer
Answer by MikeNewall · May 25, 2013 at 12:04 AM
Rigidbodies are components, you don't instantiate them. Instantiate your prefab and then search for the rigidbody component that's attached to it using GetComponent.
Edit: if it has a rigidbody attached to it you can just go
gameObject.rigidbody
No need to getcomponent
Thanks for that - I'm just getting used to it all! But your answer makes sense. Thank you.