- Home /
Null Reference Exception After refering to an Array
Hey guys im making a zombie survival game and I am facing a problem with levels. I have basicly made a menu where you can edit your player and in this menu I want my player's level to be appeared.. the problem is that i have set up the level up logic in a different script so, i am refering to it from my main menu script (as my player edit screen is in my main menu) my code is this:
public Texture[] levelTextures;
public string playerName;
public void OnGUI()
{
GameObject manager = gameObject;
PlayerController pCont = manager.GetComponent<PlayerController>();
GUI.Label(new Rect(Screen.width / 2 + 115, Screen.height / 2-25, 100,100), "Player Level", mainMenuLetters);
GUI.Label(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 35, 100,100), "[" + playerClan + "]" + playerName + ": " + pCont.playerLevel, mainMenuLetters);
GUI.DrawTexture(new Rect(Screen.width / 2 + 100, Screen.height / 2 + 35, 100,100), levelTextures[pCont.playerLevel - 1]);
}
Note***: playerLevel is an int variable.
My Error is this: NullReferenceException: Object reference not set to an instance of an object MultiplayerScript.MainMenu () (at Assets/Scripts/MultiplayerScript.cs:475)
Note2***: line 475 = GUI.Label(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 35, 100,100), "[" + playerClan + "]" + playerName + ": " + pCont.playerLevel, mainMenuLetters);
I really can't solve this problem on my own i have tried everything please help me.
is the main script attached to the game object having the PlayerController component?
because when you assign pCont you are actually doing
pCont = this.GetComponent<PlayerController>();
yeah man that was the issue.. i was refering to a script that didnt exist on that gameobject now i realised it :) thank you very much!
Answer by robusto · Jun 14, 2014 at 01:27 PM
in your inspector for this gameobject, look under this script/component and look at levelTextures array, if any of them say NONE, then they are null / not pointing to anything. Somewhere you have to load textures or set them equal to something.