Search Unity

Weird NullReferenceException Error

Discussion in 'Scripting' started by BeefJerky17, Aug 25, 2019.

  1. BeefJerky17

    BeefJerky17

    Joined:
    Jan 3, 2019
    Posts:
    3
    Hi! I have been searching every forums and document and I can't find any answers to my weird error.

    The Error is quite simple.

    First i check if the Boss Enemy spawns using this code

    Code (CSharp):
    1. public string BossName;
    2.     void Update()
    3.     {
    4.  
    5.         if (GameObject.Find(BossName))
    6.         {
    7.             Math.SetActive(true);
    8.         }
    9.         else
    10.         {
    11.             Math.SetActive(false);
    12.         }
    13.     }
    Then i enable the timer once the Math gameobject is active using this code

    Code (CSharp):
    1. [Header("TimerProperties")]
    2.     public Slider TimerSlider;
    3.     public float Timer;
    4.     private float CountDown;
    5.  
    6.     [Header("MathUi at Boss")]
    7.     public string num1;
    8.     public string num2;
    9.     void Start()
    10.     {
    11.         CountDown = Timer;
    12.         TimerSlider.maxValue = Timer;
    13.     }
    14.     void Update()
    15.     {
    16.         CountDown -= Time.deltaTime;
    17.         TimerSlider.value = CountDown;
    18.         if (CountDown <= 0)
    19.         {
    20.             CountDown = Timer;
    21.         }
    22.     }
    The prob is it somehow can't find the TimerSliderobject. What's weird is that the code is working perfectly but it spams the error NullReferenceException (I understand this since it's in the update method) Any ideas how this could be fixed?

    I'm so sorry but I'm currently working on my thesis and the deadline is already near but no matter what i try nothing seems to work. I've been awake for almost 18 hours now and my brain is now dead.

    At this point I'm now desperate for any help i can get
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    816
    Maybe you didn't slot in the slider variable in the inspector? Its not clear if you already did that from your post.
     
  3. BeefJerky17

    BeefJerky17

    Joined:
    Jan 3, 2019
    Posts:
    3
    I did slot in the slider. The script is working fine but it spams me that error




    Also i moved the code
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         CountDown = Timer;
    4.         TimerSlider.maxValue = Timer;
    5.     }
    To
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         CountDown = Timer;
    4.         TimerSlider.maxValue = Timer;
    5.     }
    Which removed the spamming NullReferencePoint. Now it only gives the error at the Awake method and still pointing to the TimerSlider.
     
    Last edited: Aug 26, 2019
  4. BeefJerky17

    BeefJerky17

    Joined:
    Jan 3, 2019
    Posts:
    3
    OK, I do not know why the bug was gone but when I added a few codes somehow the bug was removed. This is sooooooooo weird

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         boss = GameObject.Find(bossname);
    4.         CountDown -= Time.deltaTime;
    5.         PassCountDown = CountDown; //pass countdown to numbers script
    6.         TimerSlider.value = CountDown;
    7.      }
    Code (CSharp):
    1. void Start()
    2.     {
    3.         CountDown = Timer;
    4.         TimerSlider.maxValue = Timer;
    5.         GenerateNumber();
    6.     }
    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         CountDown = Timer;
    4.         TimerSlider.maxValue = Timer;
    5.     }
     
    Last edited: Aug 26, 2019