List remains null after initialisation?
Okay, so this is making me feel completely dumb. I'm making a game in which you can collect things. They have to respawn when the player loses a life. I have two C# classes. One "Umpire" class that manages things that should be maintained and managed throughout gameplay, and a "Collectible" class, which I'm having a few other classes ("Coin" and "Upgrade") inherit from.
The Umpire class is attached to a GameObject named "Umpire" in my scene, and my scene contains "Coin" and "Upgrade" objects.
In the Umpire class, I'm making a List of Collectibles that contains all Collectible objects that have been collected during the player's current life. I want to add to this from the Collectible class upon collision with the player.
The weird thing however, is that this list stays null when I try to add to it, throwing me a null reference exception! Now, this might just be my exhaustion catching up to me, but I just don't see what I'm doing wrong.
I've tried public and private (with getters and setters), static and non-static (because I just want one list, but there will only ever be one umpire object, so either should be fine), but nothing makes a difference.
I even added a useless integer to check whether I could assign and retrieve that, and I can, so there must be something particular with lists going on...
The Umpire class:
using System.Collections.Generic;
using UnityEngine;
public class Umpire : MonoBehaviour
{
public List<Collectible> collectedDuringThisLife;
public int indicator;
private void Awake()
{
collectedDuringThisLife = new List<Collectible>(); //List of collected coins. Stays null??
indicator = 6;
//DontDestroyOnLoad(transform.gameObject);
}
void Update()
{
/*
*/
}
}
The Collectible class
(inherited from by Coin and Upgrade):
using UnityEngine;
public class Collectible : MonoBehaviour {
GameObject umpire;
char letter;
private void Start()
{
umpire = GameObject.Find("Umpire"); // Get the Umpire GameObject from the Scene
Debug.Log(umpire.GetComponent<Umpire>().indicator); // Check if the integer is assigned.
//It is.
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player") // If the player touches the collectible
{
Debug.Log(umpire.GetComponent<Umpire>().collectedDuringThisLife);//.Add(this);
//Not adding right now, just checking if exists. Gives nullreference exception either way.
this.gameObject.SetActive(false); // Make it disappear
}
}
}
So yeah, I'm probably missing something obvious, but I'm at the end of my wits here. Any insight would be greatly appreciated!
I have a few questions that i would like to ask before trying to give an answer.
what is in the GetCollectedDuringThisLife() function, does it just return the list collectedDuringThisLife?
does the collectedDuringThisLife need to be static? or is it ok if you have a static class with a normal instance available inside of it?
Oh oops, that was a leftover from when I tried the private approach. I'll edit it out!
It could work as either static or non-static since I will only ever have one Umpire object at a time. So yeah, it can be removed, which I did.
Answer by kuijerlattie · Mar 22, 2018 at 09:17 AM
I wrote up a bit of code that should work, with a static instance of the Umpire class:
public class Umpire
{
private static Umpire _instance;
public static Umpire Instance
{
get
{
if (_instance == null)
{
_instance = new Umpire();
}
return _instance;
}
set
{
_instance = value;
}
}
public List<Collectible> collectedDuringThisLife;
public int indicator;
Public Umpire()
{
collectedDuringThisLife = new List<Collectible>();
Debug.Log("Instantiated collectedDuringThisLife, this should not be null: " + collectedDuringThisLife);
indicator = 6;
}
}
and a updated collectible class:
public class Collectible : MonoBehaviour
{
char letter;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
Umpire.Instance.collectedDuringThisLife.Add(this);
Debug.Log("Total items collected during this life: " + Umpire.Instance.collectedDuringThisLife.Count);
this.gameObject.SetActive(false);
}
}
}
let me know if this works :)
It no longer gives a NullReferenceException, but this code tries to create a new Umpire instance every time a coin is collected (which also results in Unity complaining that $$anonymous$$onoBehaviour classes should not be created with the new command.
A new Umpire instance being created every time also results in the list never growing.
lets stop inheriting from monobehaviour completely :) that should work. changed it in the answer too. I guess the new part failing kept the _instance null and made it reinstantiate every time.
at least, will the Umpire use any monobehaviour specific functionality in the final version? if so, then ill write a fix that keeps it a monobehaviour.
Your answer
Follow this Question
Related Questions
Null Reference Exception on Sprite Renderer 0 Answers
Having a issue with a NullReferenceException 1 Answer
Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers
NullReferenceException: When creating a graph 0 Answers
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers