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 varunvp · Dec 31, 2014 at 07:32 AM · nullreferenceexception

Weird NullReferenceException

Hey Guys/Gals, Down here is a script in which I spawn projectiles from a ProjectilePool(Object Pooling). It is attached to a space vehicle, which has a child called 'Tip'.

 void Start ()
     {
 
         pool = GameObject.FindGameObjectWithTag ("ProjectilePool").GetComponent<ProjectilePool> ();
         if(pool.poolSize <=10)
             pool.poolSize +=10;
 
         go = pool.RetrieveInstance();
         if(go)
         {
             var script = go.GetComponent<MoveForward>();
             speed = script.speed;                                //retrieves common speed of projectiles
         }
         foreach(Transform t in transform)
         {
             if(t.gameObject.name=="Tip")
             {
                 tip = t;
                 Debug.Log(tip.name);
             }
         }
     }
 
     public void Update()
     {
 
     }
     public void Fire ()
     {
         go = pool.RetrieveInstance ();

             Debug.Log(tip);
         var ray=new Ray(tip.position, tip.forward);    \\NullReferenceException
         if (go) 
         {
             go.transform.position = tip.position;
             go.transform.rotation = tip.rotation;
 
             if(Physics.Raycast(ray, out hit, 100))
             {
                 var time = hit.distance / speed;
                 StartCoroutine(CastRay(time));
             }
         }
     }

I've attached this script to a turret and it works fine, but on this new space car(still finding a name) it gives a NullReferenceException error at the aforementioned line, and Debug.Log(tip); displays Null at runtime. But Debug.Log(tip.name); displays "Tip". It's like 'tip' is losing it's value/reference between Start() and Fire(), which is called from another script. What do I do?

Comment
Add comment · Show 3
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 taxvi · Dec 31, 2014 at 07:50 AM 0
Share

try this line after the foreach in the start:

 if(tip == null){
 Debug.Log("could not find tip");
 }
avatar image Eluate · Dec 31, 2014 at 10:36 AM 0
Share

Are you assigning tip as a variable at the start of your script?

avatar image varunvp · Jan 05, 2015 at 05:15 AM 0
Share

Yes, I find any Tip in the game object and I assign it to t. Is there anything wrong with that? Thanks for the help, taxvi and StaticHS.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by varunvp · Jan 05, 2015 at 10:16 AM

This might seem dumb guys, but I just had to restart Unity. It works as expected now. Oh, the mysterious ways of the personal computer.....

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

Answer by hav_ngs_ru · Dec 31, 2014 at 12:08 PM

I guess, possible reasons are:

  • you could rewrite the tip to null anywhere else in your script; did you post all your script?

  • you could rewrite tip from another script; try to declare tip as {get; private set;} to avoid assigning it from outside (trying do this will cause compiler error);

  • you could call Fire earlier then Start (it could be if object is inactive); check Debug.Log order (Start - first, Fire - after);

less possible are:

  • do you have only one projectile launcher in scene? if more thet one - could it be that you have initiated only first of them, and trying to launch projectile from another? Try to attach name to your Debug.Log in both cases to check this...

  • there could be two scripts attached to one GO by mistake; it's very silly but I really had such mistake several times :)

cant see other reasons yet... try to check them all first...

Comment
Add comment · Show 1 · 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
avatar image varunvp · Jan 05, 2015 at 05:12 AM 0
Share

Hey hav_ngs_ru, I just had to restart Unity for it to work, however I did try out your solutions. I don't assign tip = null anywhere, and I don't feel the need to rewrite it from another script. Ordering is fine too, Start is executed once first and then "Fire,Fire,Fire,......" is executed.

However, I do have 2 projectile launchers in the scene, which may increase later. I have attached the names of the objects and both of them show up. But I am worried if this can give any problems later since they use only one projectile pool. Thanks again:-)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to set Object reference to an instance of an object 1 Answer

Null Reference Exception Error...specific error. 1 Answer

Troubleshooting desktop build receiving unhandled NULL exception 0 Answers

Debugging NullReferenceExceptions in iOS Games 2 Answers

Null Reference Problem - Emergency - Help! (same prob/diff variables) 2 Answers


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