what am i missing?

I keep getting an error saying this…

"
NullReferenceException: Object reference not set to an instance of an object
BuildVirus.Start () (at Assets/Scripts/BuildVirus.cs:14)"

i am not quiet sure what i am not assigning though, ill post a copy of my code, if someone could help me work out what i might possibly be missing on the assigning I would be very gratefull, here is the code.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class BuildVirus : BaseVariables  {

    public int value;
    public Slider mySlider;
    public float counter = 10.0f;

    void Start(){
        // set reference to slider
        mySlider = GetComponent<Slider>();
        mySlider.maxValue = counter;
        Debug.Log ("MaxVale" + mySlider.maxValue);
        mySlider.value = counter;
    }

    void Update(){
        VirusCount.text = "Virus Count: " + virus;
//        TechPoints.text = "Tech Points: " + techPoints;
    //    vpc.text = "VPC: " + virusPerClick;
    //    Debug.Log ("techPoints:" + techPoints);
    }



    public void Clicked(){
        VirusCost = 50;
    //    Debug.Log("techPoints before:"+techPoints);
            if(techPoints >= VirusCost){
            TimeCountDown();
            techPoints -= VirusCost;

            virus += 1;
            }
    //    Debug.Log("techPoints after:"+techPoints);
           
        }

    public void TechPointsClicked(){
        techPoints += techPointsPerClick;   
        {

   
    }
    }



    void TimeCountDown () {
        // decrement counter float with time it takes to draw each frame if its above 0;
        if (counter > float.Epsilon)
        {
            counter-= Time.deltaTime;
        }
        else
        {
            // if the values so low, just set to 0
            counter = 0;
        }
        // set the sliders value to counter value
        mySlider.value = counter;
       
        if (counter == 0)
        {
           
           
        }
    }







}

thanks, been working on it for most of the day now, looking at documentation, watching videos, and trying to check out what i may be missing. so far i haven’t worked it out. thanks!

mySlider.maxValue = counter; that is the line that seems to be having the issue twice in that code.

Chances are mySlider is null. Is there a Slider attached to the GameObject with this script?

yes, i belive so. i clicked on the button, and then went to the propertys on the right side, and put a new ui slider into the slider slot. is there more to it then that?

If you assigned one in the inspector then you should delete line 13.

oh ok, i thought that line is what made the option to add a slider appear in the inspector. thanks ill try that

GetComponent tells the script to go look for a slider. Since you already assigned one its not needed. What was happening is GetComponent was looking, finding none, then overwriting the Slider you assigned with null.

oh ok, thanks for explaining!

hmm, took it out and the slider doesnt move, but it adds a virus when i try purchasing one for tech points, maybe i orderd the code wrong.

think i just didnt have it set, anyways thanks for the help! :smile:

hmm… still wont start the slider…