- Home /
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); }
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!
Answer by Jessy · Jan 06, 2011 at 11:19 PM
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?