x


NullReferenceException Transform.Find

theres my code so far...But I am getting that error for the transform.find part. If I change it it works fine. thank you in advance.

var bulletPrefab:Transform;

function Awake()
{

}

function Update() 
{
    if(Input.GetMouseButtonDown(0))
    {
        Shoot();
    }
}

function Shoot()
{
        var bullet : GameObject = Instantiate(bulletPrefab , transform.Find("projRelease").transform.position ,Quaternion.identity);
        //ejectSound.Play();
        bullet.rigidbody.AddForce(transform.forward * 1000);
}
more ▼

asked Jan 06 '11 at 11:14 PM

Fierce Waffle gravatar image

Fierce Waffle
176 34 38 49

Looks like Transform.Find is returning null, which you then try to access with ".transform.position". See Jessy's answer for more details in your situation, but general it is good practice to check the return value from a function rather than reference it directly. Good luck!

Jan 07 '11 at 05:42 AM yoyo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Transform.Find can only return transforms that are children of whatever Transform you are referencing. projRelease is not the name of a child of the game object to which this script is attached. Maybe you meant to use GameObject.Find? That's what I'd assume; why else would you have used .transform.position, considering Transform.Find already returns a Transform?

more ▼

answered Jan 06 '11 at 11:19 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

(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:

x1667
x1273
x329
x307
x204

asked: Jan 06 '11 at 11:14 PM

Seen: 2153 times

Last Updated: Jan 06 '11 at 11:14 PM