- Home /
Other
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start ()
Hello. So I am trying to make an Idle game and I'm just experimenting with the progress bar stuff. So what I'm trying to do is make the progress bar autofill to 100% and every time it does that, my population gains by 1. I got the progress bar to work and auto reset but I'm struggling to pull the "population" variable from my "Population" script. I am making the progress bar in the "Progress Bar" script. But I get the error NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () (at Assets/Scripts/ProgressBar.cs:21) and I haven't found anything during over 30 minutes of search. Here are the scripts below if you guys have any ideas.
Population
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Population : MonoBehaviour {
public UnityEngine.UI.Text populationText;
public UnityEngine.UI.Text ppc;
public UnityEngine.UI.Button summon;
public float population = 0f;
public float populationPerClick;
// Large Number Variables
/*
void Update()
{
// Plurals
// Population Total
if (population == 1)
{
populationText.text = population + " human on Earth";
} else if (population == 0)
{
populationText.text = population + " humans on Earth";
} else if (population > 1)
{
populationText.text = population + " humans on Earth";
}
// Humans Per Click
if (populationPerClick == 1)
{
ppc.text = populationPerClick + " human per click";
}
else if (populationPerClick == 0)
{
ppc.text = populationPerClick + " humans per click";
}
else if (populationPerClick > 1)
{
ppc.text = populationPerClick + " humans per click";
}
public void Clicked()
{
population = population + populationPerClick;
}
}
Progress Bar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBar : MonoBehaviour {
public Transform LoadingBar;
public Transform TextIndicator;
public GameObject progressBar;
private Population populationScript;
private float newPopulation;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Start()
{
populationScript = GetComponent<Population>();
newPopulation = populationScript.population;
Debug.Log(newPopulation);
}
void Update()
{
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
TextIndicator.GetComponent<Text>().text = ((int)currentAmount).ToString() + "%";
newPopulation = newPopulation + 1;
}
else
{
TextIndicator.GetComponent<Text>().text = "Done";
currentAmount = 0;
}
LoadingBar.GetComponent<Image>().fillAmount = currentAmount / 100;
}
}
So the error occurs on line 21 in the second script (the progress bar script). Anyone got any ideas? Thanks!
Answer by Sackstand · May 28, 2017 at 08:48 AM
make sure that the populationScript is on the same object as your Progress Bar otherwise it will get an NULL back from GetComponent.
Answer by FM-Productions · May 27, 2017 at 06:46 PM
simple:
In line 20, populationScript = GetComponent<Population>();
The GetComponent function seems to return null. it cannot find the component you are looking for. You have to attach a Population component to the gameObject where you have attached the ProgressBar script. If you have the population component somewhere else, there are other ways to retrieve it from your scene.
When you said, "You have to attach a Population component to the gameObject where you have attached the ProgressBar script,"
Isn't this the right way to do it?
The name of the other script is Population.
public GameObject progressBar; private Population populationScript;
I don't know if you have attached a Population component to your GameObject that also has the ProgressBar component assigned. In the inspector view of your gameObject that has the ProgressBar script attached, you also have to attach the Population script as component for your code to work. It should look like this in the inspector:
Attaching the script as component does not happen in the code, but in the inspector view of the gameObject in Unity itself.
Imagine the Health Pool is your ProgressBar script and the Enemy script would be your Population script. They are on the same gameObject.
Also remember: A NullReferenceException occurs when you try to reference a variable that is still null (trying to access properties or functions on null will result in such an exception). So you can find out why the referenced variable is still null. In your case, GetComponent returns null if the desired component is not attached to your gameObject. If you are unsure how certain functions behave, it is a good strategy to look into the official documentation:
https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html