- Home /
Simultaneous Null Reference Exception and expected value
So I've been messing around with instantiating a prefab and then calling the components for edit to try and make save data system that creates when something is saved, and destroys when something is deleted via a toggled selection. When the game is in play mode, every frame returns both a Null Reference Exception and a true or false depending on the condition of the toggle. I'm fairly new to coding and Unity but I haven't experienced this before and I can't seem to fix it.. any and all help would be much appreciated!! Just for some reference, the Value and NewInstant functions are running off a single button, and the deleteToggled on another button aims to check the condition of the toggles and destroy instances accordingly.`using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.UI; using System;
public class Buttons : MonoBehaviour {
public GameObject Entry;
private int value = 0;
private GameObject new1;
private GameObject new2;
private GameObject new3;
private GameObject new4;
public Transform Panel;
public Toggle Toggle0;
public Toggle Toggle1;
public Toggle Toggle2;
public Toggle Toggle3;
public Toggle Toggle4;
public Text fab1;
public Text fab2;
public Text fab3;
public Text fab4;
public Text fab5;
void Start ()
{
}
public void Value ()
{
value += 1;
Debug.Log(value);
}
public void NewInstant ()
{
if (value < 5) {
if (new1 == null) {
new1 = Instantiate (Entry);
new1.transform.parent = Panel;
Toggle1 = new1.transform.GetChild(2).GetComponent<Toggle>();
fab1 = new1.transform.GetChild(0).GetComponent<Text>();
fab1.text = "This is prefab number " + value.ToString();
} else if (new1 != null) {
new2 = Instantiate (Entry);
new2.transform.parent = Panel;
Toggle2 = new2.transform.GetChild(2).GetComponent<Toggle>();
fab2 = new2.transform.GetChild(0).GetComponent<Text>();
fab2.text = "This is prefab number " + value.ToString();
} else if (new2 != null) {
new3 = Instantiate (Entry);
new3.transform.parent = Panel;
Toggle3 = new3.transform.GetChild(2).GetComponent<Toggle>();
fab3 = new3.transform.GetChild(0).GetComponent<Text>();
fab3.text = "This is prefab number " + value.ToString();
} else if (new3 != null) {
new4 = Instantiate (Entry);
new4.transform.parent = Panel;
Toggle4 = new4.transform.GetChild(2).GetComponent<Toggle>();
fab4 = new4.transform.GetChild(0).GetComponent<Text>();
fab4.text = "This is prefab number " + value.ToString();
}
}
}
void Update ()
{
if (Toggle1.isOn) {
Debug.Log (1);
} else {
Debug.Log ("not 1");
}
}
public void deleteToggled ()
{
if (Toggle1.isOn == true) {
DestroyObject (new1);
}
if (Toggle2.isOn == true) {
DestroyObject (new2);
}
if (Toggle3.isOn == true) {
DestroyObject (new3);
}
if (Toggle4.isOn == true) {
DestroyObject (new4);
}
}
} `
Answer by ShadyProductions · Feb 07, 2018 at 09:55 AM
Hi,
For example your toggle2 is part of a child of gameobject new2
Toggle2 = new2.transform.GetChild(2).GetComponent<Toggle>();
When you destroy new2 it will also destroy its children which will make toggle2 null.
This goes for all your toggles.
Hello! This is true, it makes sense before the instance has been created i.e at the start and straight after destroying, but after creating a new instance the Toggles are/should be reassigned to the new instance's children (I think), so when all 4 (or even just the first one) are active there should be no null; in the console at least.
You have to assign the new instance of the toggle back to the original reference of Toggle1 otherwise it will remain null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Closed]NullReferenceException on Object Instantiation onto game world 2 Answers
Instantiating class creating copies with same instance ID of 0 0 Answers
NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer
After Instantiating a Prefab I get a NullReference Error even though the Object was created 2 Answers
Error putting an object into an Array 0 Answers