#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,
ArgumentException: You can only call GUI functions from inside OnGUI.
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?