Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Nolirneen · Mar 21, 2011 at 04:16 AM · colormouseclickgui.button

Making GUI button change colour after click

Hello,

For my project, I've a list of GUI buttons, and I'd like to make a button turn green when it is activated.

When the user first starts the program, one button is already activated, thus it has to be green from the start already until the user turns it off.

I'm currently using this script; here's an excerpt:

if(GUILayout.Button("Light 1"))
    {
        lights[0].enabled = !lights[0].enabled; 
        function OnMouseDown () 
            {
            guiTexture.color = Color.green;
            }

I've no problems with the other scripts, only when I add in the OnMouseDown function it can't compile the script. I think I'm doing it wrong, but what's the correct way of doing it?

Appreciate the help. =)

Comment
Add comment · Show 2
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 pravin gate · Mar 21, 2011 at 04:49 AM 0
Share

if u r using javascript ins$$anonymous$$d of writing a mousedown function just try

to check this one....

if(Input.Get$$anonymous$$ouseButtonDown(0))

{

guiTexture.color=Color.green; }

avatar image Nolirneen · Mar 21, 2011 at 06:43 AM 0
Share

Appreciate the help; but it doesn't change the color to green. =\

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nolirneen · Mar 25, 2011 at 08:32 AM

I've found a solution to this. =)

I found this while looking around the site.

Based on that, I came up with this, and it works perfectly for me. =)

var lights : Light[];

 //For the custom GUI skin

var guiSkin : GUISkin;

 //For the toggle to work

var toggleBool1 : boolean = false;

 //For the size and placement of the buttons

private var toggleRect1 = Rect(25, 25, 100, 20);

function OnGUI()

{ //To change the button's texture when its activated toggleBool1 = GUI.Toggle(toggleRect1, toggleBool1, "LightSwitch 1", guiSkin.button);

//To make my light respond to the toggle switch. lights[0].enabled = toggleBool1; }

Now I can make a toggle (that looks like a button) turn green when its activated, and stay green until I click it again.

For the texture of the button when its pressed I used a .png file thats just green.

PS. Thanks Duck and everyone who helped by answering also! =D

Comment
Add comment · 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
1

Answer by Owen-Reynolds · Mar 21, 2011 at 04:52 AM

Official way is probably to use a GUI.toggle with a GUIstyle. In the inspector, OnNormal is what it looks like when it is clicked on. The scripting reference has the rest. A toggle is a button the "remembers" On/Off.

OnMouseDown is a function, so can't go inside another function. that's why it's made at you. If you really want it that way, use Input.GetButtondown(0) (or look in the Input scripting section for other options.) But, that first if is already checking the button for you.

Comment
Add comment · Show 2 · 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 Nolirneen · Mar 21, 2011 at 06:45 AM 0
Share

Hmm I tried the GetButtondown but it doesn't work; the button doesn't turn green. If I change to GUI.toggle, I'll have to change the way the switches are scripted, but I don't know how to make them work. =\

avatar image Owen-Reynolds · Mar 21, 2011 at 09:52 PM 0
Share

The Unity scripting section for GUI: http://unity3d.com/support/documentation/ScriptReference/GUI.html has code examples. I have it bookmarked.

avatar image
1

Answer by Jake-L · Mar 21, 2011 at 09:30 AM

As Owenpat said, use a toggle with a button style.

buttonstate=GUILayout.Toggle(buttonstate, "Light 1","button");

That's all you need to make a Statebutton. If you want to turn it green on activation, set it's background-color:

Color c=GUI.backgroundColor; // store value
if (buttonstate)
 GUI.backgroundColor=Color.green;
buttonstate=GUILayout.Toggle(buttonstate, "Light 1","button");
GUI.backgroundColor=c; // reset to old value
Comment
Add comment · 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 Nolirneen · Mar 22, 2011 at 03:49 AM 0
Share

Sorry but, what is buttonstate? I don't understand what if (buttonstate) means, and the other parts with buttonstate in them. Tried looking around online, couldn't find any references to it.

Also, do I declare it at the start? (This toggle "Light 1" will look like a button)?

Thanks for the help. =)

avatar image Jake-L · Mar 22, 2011 at 06:42 AM 0
Share

buttonstate is a boolean variable you declare somewhere else. It holds the state of the button (checked or not). By calling Toggle() you create a checkbox. By providing another GUIStyle (here "button") you tell the GUI to use a different style than the usual checkboxes.

avatar image Nolirneen · Mar 22, 2011 at 08:50 AM 0
Share

Ohhh I understand now. However, I've tried putting those lines into my script in different ways, but it keeps getting compiling errors. I think I'll have to find a way to change the way my button FUNCTIONS now to make it FUNCTION the same way as a toggle. Probably the lights[0].enabled part. Any ideas? =)

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

GUI button texture different color when script is on Instantiated object 0 Answers

Changing the text colour of a button removes it 0 Answers

Changing Colour of an Object With a GUI.Button Back to Original 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 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