• 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 Kyle 14 · May 13, 2011 at 02:18 PM · guiprojectlogic

Gui logic problem

Ok what i am trying to do is set it up so i can click a character then when it is clicked 2 GUI buttons pop up and ask Confirm or Go Back. The problem i am having is when you click you can click over and over again making more Gui buttons pop up. please help... heres the code: Oh yea i also want to make it so i cant click another Character while you have one clicked (There are 4 characters). Ive tried adding if statements before the buttons are created but it results in still being able to click a different character and an error that states that "GUI functions can only be called from OnGUI". Please tell me if there is a better way of going about this... :D

Script 1

var startlook : Transform;

var clickeffect : Transform;

static var clickedclass : Transform;

//Look at target

function OnMouseOver () {

Test2.targ = transform;

}

// Look away from target

function OnMouseExit () {

Test2.targ = startlook;

isover = false;

}

function OnMouseUp () {

clickedclass = transform;

     OnGUI();


// This controls the special effect for when you click

var clickedeffect = Instantiate(clickeffect,transform.position,transform.rotation);

Debug.Log("Clicked"); }

// Gui controls

function OnGUI () {

// if a class is clicked ask if you want to confirm or deny

 if(GUI.Button(Rect(10,10,60,60),"Confirm")){
 Debug.Log("Clicked Confirmed");
 }

 if(GUI.Button(Rect(10,70,60,60),"Go Back")){
 Debug.Log("Clicked Go Back");

 }

}

Script 2

static var targ : Transform;

function Update () {

//Makes what ever this script is attached to look at the target assigned

transform.LookAt(targ);

}

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

  • Sort: 
avatar image

Answer by DaveA · May 13, 2011 at 02:37 PM

Don't call OnGUI yourself, let the system do that for you.

I would have a separate class (script) that manages the GUI and character selection. It's that, or you'll have to have each character signal each other that they have been clicked to inhibit others being clicked. A 'gui manager' script can keep a variable about which (if any) character is selected and display the appropriate buttons.

Comment
GesterX
Joshua
Bunny83

People who like this

3 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is that a resolution-related issue? 1 Answer

Java code not working. LOST PROJECT 1 Answer

what is fontsize??? 2 Answers

UI components have missing scripts 5 Answers

How to start animation when GUI button is pressed 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