• 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 /
  • Help Room /
avatar image
0
Question by RNG-Development · Jun 25, 2019 at 05:39 PM · exception

Solved NullReferenceException 999+... Still all working fine and especialy in that error lines :(

All is working fine on that lines , Its calling error on IF lines on all of 4 scripts. Can't get it to disappear from console. As you can see it's all working fine, activating and deactivating child objects on 0.3 percent of shield oil rockets cannon. That scripts control scaling of Bar's with caling information from main ammo rocket shield oil system scripts. And in that IF it is taking information for percentage same as for controling scale of Bar. Just can not see where did I make a mistake. Also second and third script have reverse order in IF but that didnt change anything and it's still working as it suppoused to. So I hope someone can help me with this. (("UPDATED-This is GIF> watch yellow bar")) https://gph.is/g/apyA6l3 giphy

This is IMAGE Error-Null-02-01

first script

 using UnityEngine;
 
 public class OilBar : MonoBehaviour {
     
     private OilAmmo oilAmmo;
 
     public void Setup(OilAmmo oilAmmo) 
     {
             this.oilAmmo = oilAmmo;
             
             oilAmmo.OnOilChanged += OilAmmo_OnOilChanged;
     }
                 private void OilAmmo_OnOilChanged(object sender, System.EventArgs e) {
                 transform.Find("Bar").localScale = new Vector3(1, oilAmmo.GetOilPercent(), 1);
                 }
 
 
 
         void Update (){
          if (oilAmmo.GetOilPercent() <= 0.3)
          {
          gameObject.transform.GetChild(1).gameObject.SetActive(true);
          }
 
          else 
                 gameObject.transform.GetChild(1).gameObject.SetActive(false);
         }
 }

second script

     using UnityEngine;
     
     
     public class AmmoBar : MonoBehaviour {
         
         private GunAmmo gunAmmo;
     
         public void Setup(GunAmmo gunAmmo)
         {
                 this.gunAmmo = gunAmmo;
     
                 gunAmmo.OnAmmoChanged += GunAmmo_OnAmmoChanged;
         }
                     private void GunAmmo_OnAmmoChanged(object sender, System.EventArgs e) {
                     transform.Find("Bar").localScale = new Vector3(1, gunAmmo.GetAmmoPercent(), 1);
             }
     
      void Update ()
      {
              if (gunAmmo.GetAmmoPercent() >= 0.3)
              {
              gameObject.transform.GetChild(1).gameObject.SetActive(false);
              }
             
             else
                     gameObject.transform.GetChild(1).gameObject.SetActive(true);
     }
     
     }

third script

 using UnityEngine;
 
 
 public class RocketBar : MonoBehaviour {
     
     private RocketAmmo rocketAmmo;
 
     void Start(){}
 
 
     public void Setup(RocketAmmo rocketAmmo) 
     {
             this.rocketAmmo = rocketAmmo;
             rocketAmmo.OnRocketChanged += RocketAmmo_OnRocketChanged;
     }
                 void RocketAmmo_OnRocketChanged(object sender, System.EventArgs e)
                 {
                 transform.Find("Bar").localScale = new Vector3(1, rocketAmmo.GetRocketPercent(), 1);
                 
         }
 
      void Update (){
          
          if (rocketAmmo.GetRocketPercent() >= 0.3)
          {
          gameObject.transform.GetChild(1).gameObject.SetActive(false);
          }
 
          else 
                 gameObject.transform.GetChild(1).gameObject.SetActive(true);
         }
 }

forth script

 using UnityEngine;
 
 public class ShieldBar : MonoBehaviour {
     
     private ShieldSystem shieldSystem;
 
     public void Setup(ShieldSystem shieldSystem) 
     {
             this.shieldSystem = shieldSystem;
             
             shieldSystem.OnShieldChanged += ShieldSystem_OnShieldChanged;
     }
                 private void ShieldSystem_OnShieldChanged(object sender, System.EventArgs e) {
                 transform.Find("Bar").localScale = new Vector3(1, shieldSystem.GetShieldPercent(), 1);
                 }
 
 
 
         void Update (){
          if (shieldSystem.GetShieldPercent() <= 0.3)
          {
          gameObject.transform.GetChild(1).gameObject.SetActive(true);
          }
 
          else 
                 gameObject.transform.GetChild(1).gameObject.SetActive(false);
         }
 }
     


Comment
Add comment
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

2 Replies

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

Answer by RNG-Development · Jun 26, 2019 at 01:46 PM

I fixed this problem, when I turn of all scripts and left for shield, I get error just for first 4 seconds. and report 291 errors in that time and then stop. So It is 999+ with all four scripts but NOT infinity. Than problem solving was easy it was in my 4 seconds timer Invoke delay on player script's where i have shield and other 3 systems. So all in all Bars didnt get information on Get""Percent in that first 4 seconds and gave me this errors, and when play starts everything is working fine. I added another script on ShieldBar and other 3, with Invoke for 4 seconds, and GetComponent().enabled = false; in Start. Than on Invoke set GetComponent().enabled = false;

 using UnityEngine;
 
 public class OilBarStarter : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         Invoke ("StartBars", 4);
         GetComponent<OilBar>().enabled = false;
     }
 
     // Update is called once per frame
     void StartBars(){
         
         GetComponent<OilBar>().enabled = true;
     }
 }
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 Bunny83 · Jun 25, 2019 at 06:11 PM

I don't think that the lines with the error actually work, specifically this one:

 gameObject.transform.GetChild(1).gameObject.SetActive(true);

Since you posted a screenshot of your hierarchy we can see that all your bars do only have one child. So there is no child at index 1, only one at index 0. So you probably want to use

 transform.GetChild(0).gameObject.SetActive(true);

However this is generally not really recommended. It's better you declare a public variable and just enable / disable this one. Just something like that:

 public GameObject bar;
 
 void Update ()
 {
     bar.SetActive(oilAmmo.GetOilPercent() <= 0.3);
 }

Just remember to assign the appropriate bar child to each of your scripts.

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 RNG-Development · Jun 25, 2019 at 06:41 PM 0
Share

It have 2 childs in every OilBar ShieldBar Rocket Bar CannonBar. First child at 0 index is "Bar" Second one is at index 1.

All Warning's work on and of wich we can see in hierarchy and game screen for rockets and oil bars. Also Bar child have its own fill child that is irelevant to this.

Script that have error is atached to the Parent of Bar and Warning- OilBar, ShieldBar... , and are controling child indexed 0 but with find with name "Bar" and child indexed 1 Warning with caling on index :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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 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 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 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 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

InvalidOperationException: Operation is not valid due to the current state of the object 0 Answers

I don't know how to fix 'Transform child out of bounds' exception. 0 Answers

How can I show a prompt for the user when my standalone application crashes? 0 Answers

Catch unity generated exceptions(Texture rectangle out of bounds) 0 Answers

ArgumentOutOfRangeException: Argument is out of range. -> Index 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