I made a character script but it has an error that states “ArgumentException: InternalGetGameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Component.get_gameObject () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineComponent.cs:171)
CharacterSkin - Gui…ctor () (at Assets/Scripts/CharacterSkin - Gui.js:4)”
What happens is when I build it for web and I play it, it immeditely crashes.
So how would I remove this error?
Script:
#pragma strict
var newName : String = "What's Your Name?";
var tm : TextMesh = gameObject.GetComponent(TextMesh);
var guiSkin : GUISkin;
var ShowButton = true;
var player : GameObject;
var playerModel : GameObject;
var preview : GameObject;
var skin1 : Texture2D;
var skin2 : Texture2D;
var skin3 : Texture2D;
var btnTexture1 : Texture;
var btnTexture2 : Texture;
var btnTexture3 : Texture;
function Start()
{
player.active = false;
}
function Update () {
tm.text = newName;
}
function OnGUI() {
GUI.skin = guiSkin;
if (ShowButton) {
newName = GUI.TextField (Rect (400, 400, 200, 30), newName, 25);
if (GUI.Button(Rect(250,250,100,100),btnTexture1))
{
playerModel.renderer.material.mainTexture = skin1;
renderer.material.mainTexture = skin1;
}
if (GUI.Button(Rect(450,250,100,100),btnTexture2))
{
playerModel.renderer.material.mainTexture = skin2;
renderer.material.mainTexture = skin2;
}
if (GUI.Button(Rect(650,250,100,100),btnTexture3))
{
playerModel.renderer.material.mainTexture = skin3;
renderer.material.mainTexture = skin3;
}
if (GUI.Button(Rect(450,450,100,50),"Done"))
{
player.active = true;
Destroy (preview);
ShowButton = false;
}
}
}