• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by squeakywafflez · Jul 27, 2014 at 09:17 PM · errorguiongui

[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,

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?
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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");
Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image squeakywafflez · Jul 27, 2014 at 09:47 PM 0
Share

-_- 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

avatar image Hullu · Jul 27, 2014 at 11:12 PM 0
Share

Move 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");
avatar image squeakywafflez · Jul 28, 2014 at 01:49 AM 0
Share

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. My apoligies!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Material on GUI Transparent problem 0 Answers

My buttons do not appear when paused? 2 Answers

Hiding OnGUI? 1 Answer

Javascript GUI and deathscript weird compiler errors 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges