x


C# variable type problem, always get NullRefExc

Hello. I'm playing around in Unity3D 3. I'm rewriting this JS code:

var prefabBullet:Transform;
var shootForce:float;
function Update () 
{
    if (Input.GetButtonDown("Fire1"))
    {
        var clone = Instantiate(prefabBullet, transform.position, transform.rotation);
        clone.rigidbody.AddForce(transform.forward * shootForce);
    }
}

Into C#:

using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour {

    public Transform bullet;

    void Update() {
        if (Input.GetButtonDown("Fire1")) {

            Transform clone;

            clone = Instantiate(bullet, transform.position, Quaternion.identity) as Transform;
            clone.rigidbody.AddForce(transform.forward * 1000);
        }
    }
}

But I always get a "NullReferenceException: Object reference not set to an instance of an object" when I hit my Fire1 button. I think there is a variable type problem, but I cant solve it :( Please help. Thanks, Erhan

more ▼

asked Oct 28 '10 at 03:00 PM

Erhan gravatar image

Erhan
9 4 5 8

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

1 answer: sort oldest

Your translation isn't entirely off, but your problem is not one of type and would have happened in the original javascript.

Here's a literal translation of the first script:

using UnityEngine;

public class Shoot : MonoBehaviour {
    public Transform bullet;
    public float shootForce;

    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            Transform clone = Instantiate(bullet, transform.position, 
                                          transform.rotation) as Transform;
            clone.rigidbody.AddForce(transform.forward * shootForce);
        }
    }
}

For help with translation, this is a useful link.

Your problem is in one of these possible locations:

  • Instantiate(bullet... - is bullet set to something?
  • clone.rigidbody... - does bullet have a Rigidbody?

You can add some checks to avoid this problem altogether:

using UnityEngine;

public class Shoot : MonoBehaviour {
    public Transform bullet;
    public float shootForce = 1000.0f;

    void Update() {
        if (Input.GetButtonDown("Fire1") && bullet && bullet.rigidbody) {
            Transform clone = Instantiate(bullet, transform.position, 
                                          transform.rotation) as Transform;
            clone.rigidbody.AddForce(transform.forward * shootForce);
        }
    }
}
more ▼

answered Oct 28 '10 at 03:15 PM

skovacs1 gravatar image

skovacs1
10k 11 25 92

Thank you very much! Now it works fine! :)

Oct 28 '10 at 04:26 PM Erhan
(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:

x852
x414
x135

asked: Oct 28 '10 at 03:00 PM

Seen: 1127 times

Last Updated: Oct 28 '10 at 03:00 PM