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 Divinum Fulmen · Jul 06, 2014 at 04:18 AM · c#null

FindWithTag is returing true, and while GetComponent is returning null...seemly random

I have a boss prefab with a script that my GUI's Script calls on to display its health. My boss's script calls my GUIScript.UpdateBossShowBar on awake (which works).

bossHealthControllerObject is returning my boss prefab, but bossHealthController is returning null, but not every time. Sometimes it returns null, sometimes it finds my script.

Not sure if this helps, but it gets more confusing: I temporarily put UpdateBossShowBar (); into Update so it gets called every frame (for testing proposes), and I found the Null error fixes itself if I click anywhere in the Hierarchy or Project menus while UpdateBossShowBar is being called.

 public void UpdateBossShowBar ()
     {
         //Find the BossHealthController script
         GameObject bossHealthControllerObject = GameObject.FindWithTag ("Boss");
         if (bossHealthControllerObject != null) 
         {
             bossHealthController = bossHealthControllerObject.GetComponent <BossHealthController>();
 
             Debug.Log (bossHealthControllerObject);
             Debug.Log (bossHealthController);
         }
         if (bossHealthController == null) 
         {
             Debug.Log ("Cannot find 'BossHealthController' script");
         }
     }


Been searching answers and other sites for this all day, and can't find any solution.

Comment
Add comment · Show 2
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 tanoshimi · Jul 06, 2014 at 06:15 AM 0
Share

Did you say that UpdateBossShowBar() is being called from OnGUI()? That's a bad idea - OnGUI runs multiple times per frame. Why don't you move your FindWithTag and GetComponent calls to, e.g., the Start() function, and then just cache a reference to them to re-use in this function?

e.g.

 GameObject bossHealthControllerObject;
 BossHealthController bossHealthController;

 void Start() {
   GameObject bossHealthControllerObject = GameObject.FindWithTag ("Boss");
   if (bossHealthControllerObject != null)
   {
     bossHealthController = bossHealthControllerObject.GetComponent <BossHealthController>();
   }
 }

 void UpdateBossShowBar ()
 {
   // do stuff with bossHealthControllerObject and bossHealthController 
 }






avatar image Divinum Fulmen · Jul 06, 2014 at 06:35 AM 0
Share

Fairly new to C# and Unity, I do not know what you mean by cache a reference.

I tried putting my GamveObject.FindWithTag and GetComponent in the start loop, but then it only works if I have the GameObject with the Boss tag in the starting frame, and doesn't work as an instantiated prefab.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 06, 2014 at 06:43 AM

You have to start to properly debug. First of all it makes no sense to Debug.Log multiple values without a reference. Use a distinguishable text. Next thing is it looks like the "bossHealthController" variable is declared in the class itself. Are you sure you don't have any other code that might set / replace the content of that variables?

It might be a good idea to temporarily make "bossHealthControllerObject" and "bossHealthController" a public member variable. That way you can see in the inspector which is found and you can double click on it to see "what" it actually found. Maybe you have other things tagged with "Boss"?

 public GameObject bossHealthControllerObject;
 public BossHealthController bossHealthController;

 public void UpdateBossShowBar ()
 {
     bossHealthControllerObject = GameObject.FindWithTag ("Boss");
     if (bossHealthControllerObject != null)
     {
         bossHealthController = bossHealthControllerObject.GetComponent <BossHealthController>();
         if (bossHealthController == null)
         {
             Debug.LogWarning("The first gameobject tagged 'Boss' doesn't have a 'BossHealthController'");
         }
         else
         {
             Debug.Log("Everything ok, got 'BossHealthController'");
         }
     }
     else
     {
         Debug.Log ("Cannot find a gameobject tagged 'Boss' ");
     }
 }

Use that and see what output you get. Also at runtime check the two variables in the inspector to see their content.

Comment
Add comment · Show 4 · 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 Bunny83 · Jul 06, 2014 at 06:52 AM 0
Share

ps: If that's actually all you do in "UpdateBossShowBar" it really should have a name like

 void FindBoss()

since it doesn't update the boss nor any gui bars. All it does is getting a reference to the "next" BossHealthController.

avatar image Divinum Fulmen · Jul 06, 2014 at 07:45 AM 0
Share

I reorganized my script like you said with debugs that are more readable, and got the "The first gameobject tagged 'Boss' doesn't have a 'BossHealthController'" message (nothing new learned here, outside of better practices anyway).

I do have some things in the one else loop though, so it does have a function (enabling the GUI health bar):

 Debug.Log("Everything ok, got 'BossHealthController'");
 //Show bar
 bossHealth.enabled = true;
 bossText.enabled = true;
             
 UpdateBossHealth ();

avatar image Bunny83 · Jul 06, 2014 at 08:34 AM 0
Share

Well, have you checked what object is referenced by "bossHealthControllerObject"? Just double click it in the inspector. Are you sure that the object which is selected has a "BossHealthController" script attached? Because when GetComponent returns null, you can be sure that there is no such component.

Program$$anonymous$$g is a very exact science. Either you do something wrong or you don't understand what you're doing wrong ;)

I bet you have another object tagged "Boss" which doesn't have a BossHealthController script attached. $$anonymous$$aybe a child object of your Boss prefab?

avatar image Divinum Fulmen · Jul 06, 2014 at 10:24 PM 0
Share

You gave me an idea. I made bossHealthControllerObject public, and found the reference in editor goes "missing" (ins$$anonymous$$d of "None", which would be if nothing was found). The gameobject and script are there and haven't been deleted.

avatar image
0

Answer by Bunny83 · Jul 07, 2014 at 02:37 AM

Well, if the reference is "missing" then it's most likely has been destroyed.

Try adding an OnDestroy callback to your "BossHealthController"

 void OnDestroy()
 {
     Debug.Log("BossHealthController destroyed");
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UnityEngine Assert IsNotNull not working on empty inspector field 1 Answer

Does pattern matching verify object lifetime and null references 1 Answer

"NullReferenceException: Object reference not set to an instance of an object" with instantiate C# 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