Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sliders: Object reference not set

Discussion in 'Scripting' started by Clydey2Times, Feb 16, 2018.

  1. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    Hey all. I'm getting a NullReferenceException when I run the following code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour {
    7.  
    8.     public float maxHealth, currentHealth;
    9.     public Slider healthSlider;
    10.  
    11.     Attackers attackers;
    12.  
    13.     void Start()
    14.     {
    15.         attackers = GameObject.Find("Lizard").GetComponent<Attackers>();
    16.         healthSlider.maxValue = maxHealth;
    17.         healthSlider.value = maxHealth;
    18.     }
    19. }
    I'm dragging the slider in from the inspector and it seems to be there. It accepts the slider I drag in, but it's not recognising it when I run the game. Any ideas?

    By the way, the canvas is in world space. I'm trying to put a slider over the head of an object. Is the issue that I'm dragging in a slider that's a child of the canvas, rather than an object?

    Edit: If I use GameObject.FindObjectOfType<Slider>() to get the reference it works fine. Dragging it in the inspector just doesn't work for whatever reason. Would still like to know why, if anyone has any ideas.

    Edit: Sorry for all the edits. Just thinking out loud. If I dragged in the canvas, would that have worked since the Slider is a component and not an object?
     
    Last edited: Feb 16, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are you sure you're getting it for the slider and not the 'attackers'?

    It's no problem to drag a child of the canvas.
     
  3. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    What do you mean? Apologies. As you know, I'm still a newbie. ;)

    I'm dragging in the slider from the canvas into where it asks for the slider in the inspector. It accepts the slider, but when I start the game I get the error.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well I saw your edits now. Are you dragging the slider to a prefab (asset) by any chance, instead of an object in the scene?
     
  5. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    No. The object is prefabbed, but I'm dragging it onto an object that's currently in the scene.
     
  6. Clydey2Times

    Clydey2Times

    Joined:
    Oct 24, 2017
    Posts:
    242
    By the way, in case anyone needs an answer to this or makes the same mistake I did. I forgot that I'd put the script on multiple objects, so about 7 of them didn't have the slider reference.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah yes, that can happen. :) Glad you got it worked out.