I’ve looked at all the similar cases and I’m afraid I don’t understand how others [SOLVED] this:
NullReferenceException: Object reference not set to an instance of an object
Value+d__8.MoveNext () (at Assets/Scripts/Value.cs:53)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Value : MonoBehaviour
{
[SerializeField]
private bool sizeMass;
public int value;
private Text displayNumber;
[SerializeField]
public GameObject underline;
private int maxValue;
[SerializeField]
private int difficulty;
private Rigidbody2D rb;
// Start is called before the first frame update
void Awake()
{
StartCoroutine(Assign());
}
private IEnumerator Assign() // creates a random value on a cargo piece,
//based on needs of the current equation and difficulty
{
int colorMe = GetComponent<ColorMe>().leftOrRight;
yield return new WaitForSeconds(.1f);
if (colorMe == 2)
{
GameObject leftovers = GameObject.Find("Killzone Green");
Restarter restarter = leftovers.GetComponent<Restarter>();
maxValue = restarter.leftover + difficulty;
}
if (colorMe == 1)
{
GameObject leftovers = GameObject.Find("Killzone Pink");
Restarter restarter = leftovers.GetComponent<Restarter>();
maxValue = restarter.leftover + difficulty;
}
if (value == 0)
{
if (maxValue >= 11)
{ maxValue = 11; }
if (maxValue <= 0)
{ maxValue = 1; }
value = Random.Range(1, maxValue);
if (value == 9 || value == 6)
{
underline.SetActive(true);
}
}
displayNumber = transform.GetComponentInChildren<Text>();
displayNumber.text = "" + value;
if (sizeMass)
{
transform.localScale += new Vector3(value/7f, value/7f, 0);
rb = GetComponent<Rigidbody2D>();
rb.mass = rb.mass*value/3;
}
}
private void Update()
{
if (transform.position.y < -30)
{
Destroy(gameObject);
}
}
}
The error is in:
if (value == 9 || value == 6)
{
underline.SetActive(true);
}
I have the underline assigned through inspector.
How to solve this ?
P.S. I know the code is horrible. It’s an old project and I’ve learned a lot since then.
UnassignedReferenceException: The variable underline of Value has not been assigned.
You probably need to assign the underline variable of the Value script in the inspector.
Value.Awake () (at Assets/Scripts/Value.cs:23)
UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)
spawn:Update() (at Assets/Scripts/spawn.cs:71)
Which is:
void Awake()
{
underline.SetActive(false); // <<< THIS
StartCoroutine(Assign());
}
Oh, I have many instances of the class. Usually I had no trouble having several gameobjects with the same class perform similar/same operations on objects with the same names. I need a singleton here? That’s odd…
Yes, indeed it’s on many game objects. On every single object that spawns as “cargo” … and they keep spawning all the time. But I actually think the last time I was testing (showing the game) to some friends, the errors were miraculously gone.
I will have to check and get back to you on this.
If this is the case, then the previous step would have solved it
(having it enabled first, then disabling/enabling later in code).
Sorry for being slow, life is a mess at the moment.
The gameobject which was assigned in the inspector (Underline) was disabled at the Start/Awake. So if you want to enable an object in the scene, you need to first disable it at Awake, then enable it again later.
I didn’t expect this to be the case because in certain cases it wouldn’t be a very optimal solution.