[Begginer] NullReferenceException occurs

Hello everyone,

I want just to say first that I am a begginner. I’m creating a 3d web app and my code throws a NullReferenceException. I was thinking that could be a general case:

NullReference Exception: Object reference not set to an instance of an Object
UnityEngine.GUILayoutUtility.EndLayoutGroup()

The thang is that there is no line to check out to fix this.

I was thinking that was because of the way I affect the GUI.skin property. I make that inside OnGUI function. Which is called at each frame… (?)

function OnGUI()
{
    GUI.skin = mySkin;
}

Is it a proper practice?

Thanks for your attention.
Regards,
Odissey

Hi! UP! :smile:

Since you haven’t posted more of your code I can’t say for sure what is causing your problem.

First, you only have to set the GUI skin once for it to work, so do that in Awake() or Start().

Next, if your error is indeed caused by the code you posted, then it is probably because the mySkin variable contains nothing. A NullReferenceException is a compilers way of saying: “Hey, this variable is empty, I can’t use it!”.