- Home /
Null Reference Exception Hunt !
NullReferenceException: Object reference not set to an instance of an object FireBlaster.Update () (at Assets/Scripts/FireBlaster.cs:59)
Hi there, I'm following a tutorial on YT by Gamer To Game Developer, and I've been scratching my head over this Null Reference Exception for a day and a half now. I've tried every capitalization combo and tracing each line of code before and after the error, but I can't seem to find a thing. The game does exactly what it's supposed to as of now, but I think leaving an error be is just asking for trouble in the future. I'd appreciate any help!
Code:
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the player and allows
/// them to fire the Blaster projectile.
/// </summary>
public class FireBlaster : MonoBehaviour {
//Variables Start___________________________________
//The blaster projectile is attached to this in the
//inspector
public GameObject blaster;
//Quick references.
private Transform myTransform;
private Transform cameraHeadTransform;
//The position at which the projectile should be
//instantiated.
private Vector3 launchPosition = new Vector3();
//Used to control the rate of fire.
private float fireRate = 0.2f;
private float nextFire = 0;
//Variables End_____________________________________
// Use this for initialization
void Start ()
{
myTransform = transform;
cameraHeadTransform = myTransform.FindChild("CameraHead");
}
// Update is called once per frame
void Update ()
{
if(Input.GetButton("Fire Weapon") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
//The launch position of the projectile will be just in front
//of the CameraHead.
launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);
//Create the blaster projectile at the launchPosition and tilt its angle
//so that its horizontal using the angle eulerAngles.x + 90.
Instantiate(blaster,launchPosition,Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
myTransform.eulerAngles.y, 0));
}
}
}
So what I have here is essentially code that is controlling what speed, angle, and location my blaster (a GameObject, for reference) fires from.
If you click in error message on Unity3D what line it shows in code editor?
Error message means that it did not find reference, reference is null
Forgive me for not knowing how to address that, I'm quite new to C#. What reference am I missing, or is it telling me?
It says the error is on line 59.
It is saying the error is on line 59. It most likely means that 'cameraHeadTransform' is null. That would happen if the initialization of 'cameraHeatTransform' on line 46 failed. The Transform.FindChild() function is not documented in the reference. Poking around I found this quote:
Transform.FindChild() is no longer part of the documentation, but still exists as part of the UnityEngine assembly. It is depreciated and is functionally identical to Transform.Find().
So if Transform.FindChild() is the same as Transform.Find(), then this code is expecting an immediate child object with the name 'CameraHead'.
It meants that you have a variable that is referencing something but that something doesn't exist or is Null, in this case cameraHeadTransform is null on line 59.
On line 46,
cameraHeadTransform = myTransform.FindChild("CameraHead");
that FindChild doesn't find anything and that is why it is null. are u sure the GameObject is called "CameraHead"? Is this script attached to its parent?
Answer by Addyarb · Mar 02, 2014 at 08:43 PM
To answer this, my script was duplicated somewhere else inside the hierarchy and just needed to be deleted.