I’m new to scripting and I learned a bit with the help of ChatGPT, but I can’t figure out why my script doesn’t work. I’m making a rock paper scissors game with floating objects that attack eachother, and I made a GUI scale option, but when I hit play, I get this error repeating again and again:
NullReferenceException: Object reference not set to an instance of an object
menuVals.Update () (at Assets/Scripts/menuVals.cs:68)
I don’t know what’s wrong, the references are assigned via the editor, and even if I use GameObject.Find instead of the variables, it still doesn’t detect it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class menuVals : MonoBehaviour
{
public GameObject rockSlider;
public GameObject paperSlider;
public GameObject scissorsSlider;
public GameObject rockText;
public GameObject paperText;
public GameObject scissorsText;
public GameObject GOBJrockEnabled;
public GameObject GOBJpaperEnabled;
public GameObject GOBJscissorsEnabled;
public GameObject uiCanvas;
public GameObject guiScaleOBJ;
public bool rocksEnabled;
public bool papersEnabled;
public bool scissorsEnabled;
private int rocksVal;
private int papersVal;
private int scissorsVal;
public int guiScale;
public int enabledTotal;
private float rawRocksVal, rawPapersVal, rawScissorsVal;
// Start is called before the first frame update
void Start()
{
GOBJrockEnabled.GetComponent<Toggle>().isOn = PlayerPrefs.GetInt("RocksEnabled", 0) == 1;
GOBJpaperEnabled.GetComponent<Toggle>().isOn = PlayerPrefs.GetInt("PapersEnabled", 0) == 1;
GOBJscissorsEnabled.GetComponent<Toggle>().isOn = PlayerPrefs.GetInt("ScissorsEnabled", 0) == 1;
rockSlider.GetComponent<Scrollbar>().value = (PlayerPrefs.GetFloat("Rocks", 0.0f));
paperSlider.GetComponent<Scrollbar>().value = (PlayerPrefs.GetFloat("Papers", 0.0f));
scissorsSlider.GetComponent<Scrollbar>().value = (PlayerPrefs.GetFloat("Scissors", 0.0f));
}
// Update is called once per frame
void Update()
{
enabledTotal = 0;
rocksEnabled = GOBJrockEnabled.GetComponent<Toggle>().isOn;
papersEnabled = GOBJpaperEnabled.GetComponent<Toggle>().isOn;
scissorsEnabled = GOBJscissorsEnabled.GetComponent<Toggle>().isOn;
if (rocksEnabled == true) {
enabledTotal++;
}
if (papersEnabled == true) {
enabledTotal++;
}
if (scissorsEnabled == true) {
enabledTotal++;
}
guiScale = guiScaleOBJ.GetComponent<Dropdown>().value;
uiCanvas.GetComponent<CanvasScaler>().scaleFactor = guiScale;
rawRocksVal = rockSlider.GetComponent<Scrollbar>().value;
rawPapersVal = paperSlider.GetComponent<Scrollbar>().value;
rawScissorsVal = scissorsSlider.GetComponent<Scrollbar>().value;
rocksVal = (int)(rawRocksVal * 124 + 1);
papersVal = (int)(rawPapersVal * 124 + 1);
scissorsVal = (int)(rawScissorsVal * 124 + 1);
rockText.GetComponent<TextMeshProUGUI>().text = rocksVal.ToString();
paperText.GetComponent<TextMeshProUGUI>().text = papersVal.ToString();
scissorsText.GetComponent<TextMeshProUGUI>().text = scissorsVal.ToString();
PlayerPrefs.SetInt("RocksEnabled", rocksEnabled ? 1 : 0);
PlayerPrefs.SetInt("PapersEnabled", papersEnabled ? 1 : 0);
PlayerPrefs.SetInt("ScissorsEnabled", scissorsEnabled ? 1 : 0);
PlayerPrefs.SetFloat("Rocks", rawRocksVal);
PlayerPrefs.SetFloat("Papers", rawPapersVal);
PlayerPrefs.SetFloat("Scissors", rawScissorsVal);
}
}