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 JannickBremm · Jun 06, 2014 at 07:22 PM · errorraycasterrormessageobject-reference-error

Object reference not set to an instance of an object... again

So first what is the goal of my code? Helicopter 1 fires a rocket on helicopter 2. In flight the rocket shoots a ray onto the target helicopter. Now i want helicopter 2 to recognize when a rocket is flying towards him so i wrote this:

 if(Physics.Raycast(transform.position, fwd, out hit))
 {               
     hit.collider.gameObject.GetComponent<damageHandler().RocketAlert();
 }

the method RocketAlert:

 public void RocketAlert()
 {
     Debug.Log("ROCKET ALERT!!!");
     Instantiate(flare, flareLauncher.transform.position, Quaternion.identity);
 }

So that was what i wanted to do and what did i get? The good old "Object reference not set to an instance of an object" error. What i already checked: Helicopter 2 (whos being shot at) got the damageHandler script attached.

Iam close to killing myself i worked on this little F** error for 3 or 4 hours now and i dont now what to do anymore... HELP ME!

btw sorry for bad english iam from germany...

greetz Jannick

Comment
Add comment · Show 4
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 Landern · Jun 06, 2014 at 07:41 PM 1
Share

how about including as much of the stack trace as possible and the actual lines in script it's referring to, looking at what you posted, it could be the flare variable, flareLauncher, etc etc. Please add more specificity to your question. Null exception questions happens here continuously all day, every day.

avatar image NoseKills · Jun 06, 2014 at 07:43 PM 1
Share

Which line in the code is the Exception pointing to?

Once you know the line number, you can Debug.Log() the values of all the variables accessed on that line. That should take you closer to solving the problem already. That's "debugging 101" :)

avatar image tanoshimi · Jun 06, 2014 at 08:31 PM 1
Share
 hit.collider.gameObject.GetComponent<damageHandler().RocketAlert();

Is not syntactically correct - you're missing the closing angle bracket after damageHandler.

avatar image JannickBremm · Jun 07, 2014 at 02:39 PM 0
Share

nose kills: i will try that... tanoshimi: that was a typing error here in the forum in the code its right... landern: yep it does -.- more info about the code and error below...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JannickBremm · Jun 07, 2014 at 02:40 PM

sure here is the hole code...

rocket script:

 using UnityEngine;
 using System.Collections;
 
 public class rocket : MonoBehaviour 
 {
     RaycastHit hit;
     Vector3 fwd;
 
     void Start ()
     {
         fwd = transform.TransformDirection(Vector3.forward);
     }
 
     void Update () 
     {
         rigidbody2D.AddForce(Vector2.right * 800 * Time.deltaTime);
 
         if(Physics.Raycast(transform.position, fwd, out hit))
         {
             hit.collider.gameObject.GetComponent<damageHandler>().RocketAlert();
         }
     }
 }


AI script (enemy):

 using UnityEngine;
 using System.Collections;
 
 public class AI : MonoBehaviour 
 {
 
     public GameObject launcher;
     public GameObject gun;
     Vector3 fwd;
     RaycastHit hit;
 
     void Start ()
     {
         fwd = transform.TransformDirection(Vector3.forward);
     }
 
     void Update () 
     {
         if(Physics.Raycast(transform.position, fwd, out hit))
         {
             Debug.Log(hit.collider);
             if(hit.transform.tag == "Player")
             {
                 Debug.Log("hit player");
                 Instantiate(gun,launcher.transform.position,Quaternion.identity);
             }
 
         }
     }
 }


The syntax error was a copy and paste mistake here in the forum in the code its all written right...

The Error is pointing to following line in the rocket script:

 hit.collider.gameObject.GetComponent<damageHandler>().RocketAlert();

The exact error message says: "NullReferenceExeption: Object reference not set to an instance of an object rocket.Update() (at Assets/Scripts/rocket.cs:20)

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

24 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 avatar image avatar image

Related Questions

Errors that I don't know how to fix 1 Answer

Line Renderer not showing 0 Answers

Help!Error CS0266 0 Answers

Every time I try to open a javascript I get an error saying some confusing things that I will post below. What do they mean, and how do I fix the error? 2 Answers

Object reference not set to an instance of an object 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