- Home /
[Solved] GUI Error?
#pragma strict
var inspect_object_text_1 : GameObject;
var inspect_sound1 : AudioClip;
private var canHover : boolean = false;
private var inspect_1 : boolean = false;
function Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast(transform.position, fwd, hit))
{
if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "clock")
{
canHover = true;
if(Input.GetKeyDown("e"))
{
audio.PlayOneShot(inspect_sound1);
GUI.Box(Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "urafgt");
inspect_1 = true;
//Do something!
}
}
else
{
canHover = false;
}
}
}
function OnGUI()
{
if(canHover == true)
{
GUI.Box(Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "Press 'E' To Inspect");
}
if(inspect_1 == true)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "locked.");
}
}
So I have this script, which I stick on a camera, and I am attempting to 'inspect' a clock. I have a variable 'inspect_1' which is a boolean, and when the raycast is true, and the player is close enough, and you press 'e', 'inspect_1' becomes true. at the bottom, when 'inspect_1' is true, I have it show a GUI of the text I want to appear. When I press E I get an error that reads,
UnityEngine.GUIUtility.CheckOnGUI () (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/GUIUtility.cs:229) UnityEngine.GUI.Box (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/GUI.cs:373) UnityEngine.GUI.Box (Rect position, System.String text) (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/GUI.cs:366) Inspect.Update () (at Assets/Standard Assets/Scripts/Camera Scripts/Inspect.js:23) What can I do about this?ArgumentException: You can only call GUI functions from inside OnGUI.
Answer by tanoshimi · Jul 27, 2014 at 09:23 PM
You need to remove line 23 which is attempting to use GUI in the Update() method:
GUI.Box(Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "urafgt");
-_- Okay, but that line is obviously important.. How would I get it to display the text I want when I press E then..? Edit: I didn't even see that line there I thought you mean the part I was talking about, at the bottom
$$anonymous$$ove it to OnGUI() and use boolean for example:
if(inspect_1)
GUI.Box(Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 150, 20), "urafgt");
It is in OnGui, isn't it? I'm pretty sure it is. edit: I'm talking about the bottom part of the script where it says 'locked'. The part in the middle was there by accident. $$anonymous$$y apoligies!