x


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."

more ▼

asked Jul 20 '11 at 01:51 PM

TheKnight gravatar image

TheKnight
21 13 14 15

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.
more ▼

answered Jul 20 '11 at 02:16 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 20 '11 at 02:02 PM

TheKnight gravatar image

TheKnight
21 13 14 15

Add comments as comments, not as answers.

Jul 20 '11 at 02:24 PM almo

I cannot seem to add a comment to the replies i get that's why i'm posting as answers

Jul 20 '11 at 02:28 PM TheKnight

Just click the "Add New Comment" button.

Jul 20 '11 at 02:47 PM almo

To me this only seems to work on my posts not for example on aldonaletto post.

Jul 20 '11 at 03:04 PM TheKnight

If it's like that, isn't that a little stupid? Users get downvoted just because they are posting new replies instead of commnents while they can't post.

Jul 21 '11 at 12:00 AM TheKnight
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 20 '11 at 02:21 PM

TheKnight gravatar image

TheKnight
21 13 14 15

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)

Jul 20 '11 at 02:58 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3569
x1198
x298
x153

asked: Jul 20 '11 at 01:51 PM

Seen: 1351 times

Last Updated: Jul 21 '11 at 12:10 AM