- Home /
NullRefrenceExeption on Awake and in some other places
In a dart game I'm making in 3d I have a static class for which I go for references of objects in the game. Here it is:
public class Reference : object
{
public static ScoreTextScript scoreText = GameObject.FindGameObjectWithTag("ScoreText").GetComponent<ScoreTextScript>();
public static ForceTextScript forceText = GameObject.FindGameObjectWithTag("ForceText").GetComponent<ForceTextScript>();
public static Shoot shoot = GameObject.FindGameObjectWithTag("Player").GetComponent<Shoot>();
public static GameObject[] GetDartsInScene()
{
return GameObject.FindGameObjectsWithTag("Projectile");
}
public static GameObject[] GetTargetsInScene()
{
return GameObject.FindGameObjectsWithTag("Targets");
}
}
I call some it's variables and methods almost in every script (later I'll give the places where I call it). When the game runs ~3 Errors like this are given immediatly (these are the full details):
Reference..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Reference Level_LimitedDarts.Awake () (at Assets/Scripts/GameController/Level Scripts/Level_LimitedDarts.cs:14) Afterwards more errors like this are given every frame. Here are the awake methods of the class and its base class: protected virtual void Awake() { Reference.scoreText.ScoreChanged += (int Score) => score = Score; score = 0; screenFade = GameObject.FindGameObjectWithTag("ScreenFader").GetComponent(); screenFade.FadedOut += () => WinState(winState);//Thus endeth thy scene sceneEnds = false; winState = false; } protected virtual void Awake() { Reference.scoreText.ScoreChanged += (int Score) => score = Score; score = 0; screenFade = GameObject.FindGameObjectWithTag("ScreenFader").GetComponent(); screenFade.FadedOut += () => WinState(winState);//Thus endeth thy scene sceneEnds = false; winState = false; } All of the events and variables mentioned in these methods exist. I've tried re-adding all the scripts mentioned. I've tried removing the 'protected' and/or the 'override'. Thanks in advance.NullReferenceException: Object reference not set to an instance of an object
Answer by potatoking · Dec 30, 2013 at 09:34 PM
Maybe the gameobject you are trying to find with findgameobjectwithtag() is not instantiated yet, so you can't find anything.
Answer by amelnik · Dec 31, 2013 at 05:46 PM
If so, how can I solve the problem? When I debugged the project the first time it said that the value of Refrence.shoot (for example) is reference.shoot or something like that. When I debug it the breakpoint in the Reference script is pink and a non-functioning. When I remove it the debugging still does nothing. sigh.