Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Addyarb · Mar 02, 2014 at 07:42 PM · gamenullreferenceexceptionshooting

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.
 
 
 
 
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vardan Meliksetyan · Mar 02, 2014 at 07:48 PM 0
Share

If you click in error message on Unity3D what line it shows in code editor?

avatar image Vardan Meliksetyan · Mar 02, 2014 at 07:49 PM 0
Share

Error message means that it did not find reference, reference is null

avatar image Addyarb · Mar 02, 2014 at 07:51 PM 0
Share

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.

avatar image robertbu · Mar 02, 2014 at 08:09 PM 0
Share

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

avatar image thaiscorpion · Mar 02, 2014 at 08:11 PM 1
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only one of all my Enemies shoots, Help please. 1 Answer

raycast shooting 0 Answers

Why does my Bullet prefab go missing from my gun script when destroyed? 1 Answer

How to make my space ship shooting (2D Game)? 0 Answers

How do I create a 2d shooting mechanic (Left and Right)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges