- Home /
Shooter script
I am trying to do simple throwing of crates and seen this from tornado twins. Here is the script
var speed = 3.0; var
cratePrefab:Transform;
function Update () {
// find out if a fire button is pressed
if(Input.GetButtonDown("Fire1")){
// create the prefab
var crate = Instantiate(cratePrefab,transform.position,Quaternion.identity);
// add force to the prefab
crate.Rigidbody.AddForce(transform.forward * 2000);
} }
and it gets error : "Null Reference exception: Object Reference not set to an instance of an object."
Answer by aldonaletto · Jul 20, 2011 at 02:16 PM
You've wrote crate.Rigidbody while the correct is crate.rigidbody (Rigidbody is a type). The correct form is:
crate.rigidbody.AddForce(transform.forward * 2000);
Remeber also that your
crate prefab must have a rigidbody for this to work.
Answer by TheKnight · Jul 20, 2011 at 02:02 PM
It doesn't show anything still does the same thing. It is shooting it's just shooting up along with the y axis and it's shooting with an akward interval. Shoots one that doesn't go normal and stops and when second is shot it pushes the first.
I cannot seem to add a comment to the replies i get that's why i'm posting as answers
To me this only seems to work on my posts not for example on aldonaletto post.
If it's like that, isn't that a little stupid? Users get downvoted just because they are posting new replies ins$$anonymous$$d of commnents while they can't post.
Answer by TheKnight · Jul 20, 2011 at 02:21 PM
aldonaletto - That did the trick. But in the tutorial I watched it was Rigidbody and it worked. It's case sensitive? In the editor if I write it as Rigidbody it turns blue, if not it doesn't turn any color.
Yes, Unityscript is case sensitive: Rigidbody is a type, while rigidbody is one of the transform variables. I don't know why the editor didn't turned it to blue - it should, since it's one of the words it "knows" (it's blue in my editor).
NOTE 1: you may notice the crate is always facing the same axis, no matter to what direction it's thrown. Change Quaternion.identity to transform.direction in Instantiate to fix this.
NOTE 2: Unity Answers is different from forums - you must use "Your answer" only to answer to the question; everything else must be posted with the "comment" button in order to not confuse other people (they will think this question have 3 different answers, for instance)
Your answer
Follow this Question
Related Questions
Gun Firing help? 2 Answers
fps shooting in the direction of character main cam 1 Answer
Swich fire button form F to LMB 1 Answer
An instance of type (Script) is required to access non static member (Script) 1 Answer
Checkpoint script? 1 Answer