- Home /
Question by
David Drake · May 10, 2015 at 10:31 PM ·
nullreferenceexceptionclassesstringseasyannoying
NullRerenceException On comparisons
I'm trying to compare two strings I keep getting this error. The class "LevelStruct" is also shown below. I'd appreciate for a quick and simple answer to solve this.
using UnityEngine;
using System.Collections;
public class AppManager : MonoBehaviour {
public GameObject GameController_Obj;
private GameController_V3 GameController_Script;
SceneList LevelList= new SceneList();
public LevelStruct Level_1=new LevelStruct();
// Use this for initialization
void Start () {
Level_1=LevelList.LevelPick (1);
GameController_Script = GameController_Obj.GetComponent<GameController_V3> ();
// GameController_Script.LevelDetails = Level_1;
//GameController_Script.SetLVLDetailvs(Level_1);
//GameController_Script.LevelDetails.CopyLevel (Level_1);
}
// Update is called once per frame
void Update () {
if (GameController_Script.LevelDetails.Name==Names.empty) {
GameController_Script.LevelDetails = Level_1;
Debug.Log("Level Loaded");
}
}
}
using UnityEngine;
using System.Collections;
public class LevelStruct : MonoBehaviour {
public string Name;
public int Number;
public int Lives;
public CameraPosSctruct GameCameraPos;
public CameraPosSctruct MenuCameraPos;
public LevelStruct(){
Name = "";
Number = new int();
Lives = new int();
GameCameraPos = new CameraPosSctruct ();
MenuCameraPos = new CameraPosSctruct ();
}
public LevelStruct(string Name_, int Number_, int Lives_, CameraPosSctruct GameCameraPos_,CameraPosSctruct MenuCameraPos_){
Name = Name_;
Number = Number_;
Lives = Lives_;
GameCameraPos = GameCameraPos_;
MenuCameraPos = MenuCameraPos_;
}
public void CopyLevel(LevelStruct FromLVL){
Name = FromLVL.Name;
Number = FromLVL.Number;
Lives = FromLVL.Lives;
GameCameraPos = FromLVL.GameCameraPos;
MenuCameraPos = FromLVL.MenuCameraPos;
}
}
Comment
Can you copy/paste the entire NullReferenceException error?
Without the entire stack call I'm uncertain as to the source of the problem.
But so far, from what I can tell, "Names" is a null-defined variable - as in, it isn't instantiated. Or "Names.empty" isn't instantiated.
Names.empty = ""
the problem is that GameController_Script.LevelDetails
might not have been initialized. And I wanted to detect that case.
Still thank you for the reply, I was able to go around the problem.