reference to slider error

I have made a health hud using a slider but I get the error:
Assets/Scripts/PlayerScripts/PlayerHealth.js(3,17): BCE0018: The name ‘Slider’ does not denote a valid type (‘not found’). Did you mean ‘UnityEngine.UI.Slider’?

my script:

var Health = 100;
var player : GameObject;
var healthBar : Slider;
function ApplyDammage1 (TheDammage : int)
{
        Health -= TheDammage;
        healthBar.value = Health;
      
        if(Health <= 0)
        {
                Dead();
        }
}

function Start()
{
    healthBar = Slider;
}
function Dead()
{
        Destroy (player);
        Application.LoadLevel(3);
}

did you add UnityEngine.UI; on top of your script

yep, and i got the error :
Assets/Scripts/PlayerScripts/PlayerHealth.js(18,36): BCE0022: Cannot convert ‘System.Type’ to ‘UnityEngine.UI.Slider’.

healthBar = Slider;
This lines tries to assign a component type to a variable. healthBar is already a slider.

i changed my code to

var Health = 100;
var player : GameObject;
var healthBar : UnityEngine.UI.Slider;
function ApplyDammage1 (TheDammage : int)
{
        Health -= TheDammage;
        healthBar.value = Health;
      
        if(Health <= 0)
        {
                Dead();
        }
}


function Dead()
{
        Destroy (player);
        Application.LoadLevel(3);
}

but now I’ve got the error ’ NullReferenceException: Object reference not set to an instance of an object ’

nevermind, I got it working by deleting the function start and putting the ‘healthBar.value = Health;’ line into a function update

1 Like