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);
        }
}

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.

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;
    }
}