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 atting · Apr 27, 2015 at 04:00 PM · c#unity 5javascriptgui

How To make a Ui button Chance colours when pressed

I want a UI button to be able to chance between 3 different colours when it gets pressed/touched, I want it to loop between them forever. I have been looking all over internet for hours but can't find anything. I'am very new to both Unity and C#. Thanks for 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 fffMalzbier · Apr 27, 2015 at 04:01 PM 0
Share

A good question, i did tests myself and got wired results that are not appropriate to answer the question.

avatar image AlwaysSunny · Apr 27, 2015 at 10:01 PM 1
Share

Please do not post the same question twice. It wastes everyone's time. I answered and deleted the duplicate.

Put your colors in an array and let the button invoke some method that cycles an indexer with each button press.

2 Replies

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

Answer by bilalitani · Apr 27, 2015 at 04:58 PM

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.UI;


 
    public class ColorChanger: MonoBehaviour
    {            

         List<Color> colors = new List<Color> { Color.red, Color.blue, Color.green };
 
         int colorIndex = 0;
     
         // Returns next color in the list
         Color GetNextColor()
         {
             // Get the color at colorIndex in the list
             Color nextColor = colors[colorIndex];
     
             // If we are less than the last index
             if (colorIndex < colors.Count - 1)
             {
                 // Add one
                 colorIndex++;
             }
             else
             {
                 // Otherwise restart
                 colorIndex = 0;
             }

             // Return the chosen color
             return nextColor;
         }
     
         // Attach this function to OnClick in the inspector
         public void ChangeColor()
         {
             // Set the button's color to GetNextColor();
             GetComponent<Button>().image.color = GetNextColor();
         }
     }
 




Comment
Add comment · Show 5 · 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 atting · Apr 27, 2015 at 09:12 PM 0
Share

Thank you for the quick answer! but I got an error:

NullReferenceException: Object reference not set to an instance of an object ColorChange.ChangeColor () (at Assets/ColorChange.cs:39) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)

do you know what it is?

avatar image bilalitani · Apr 27, 2015 at 09:43 PM 1
Share

That's weird; I just tested it and it works just fine. However I did forget to mention that the ChangeColor function is supposed to be public so you can assign it in the inspector (just fixed that in the original code). After that, you should create a button, attach the ColorChanger script to it as a component, and then drag the Button gameobject into the OnClick event of the button and choose the ChangeColor function. Everything should work fine after that. The Inspector should look like this:

alt text

color.jpg (83.1 kB)
avatar image atting · Apr 27, 2015 at 10:06 PM 0
Share

Thank you so much! It works now!

I have one more question for you if would be so kind to answer.

I have another button in my scene that changes to "scen02" and hat works fine , and then I have a button that takes me back to scen01.

The robe is that when I go back to scen01 from scen02 the color chancing button dosnt remember the color it was ons it goes back frome the beginning... is it a way to make the color chancing button to remember while you can jump back and forword between scen01 and scen02?

Thank you so much for the help Automatic!

avatar image atting · Apr 28, 2015 at 03:32 AM 0
Share

Thank you Automatic,

but i got an error? you know how to solve it?

NullReferenceException: Object reference not set to an instance of an object ColorChange.ChangeColor () (at Assets/ColorChange.cs:39) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)

avatar image bilalitani · Apr 28, 2015 at 04:10 AM 0
Share

See this tutorial for more info about it not saving the color between scenes: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

You'll need to tell the button somehow to remember either through playerprefs, saving data to a file, or just not destroying the button upon loading (if you wanted to hide it, you could just disable it). Good luck.

avatar image
0

Answer by Calif96 · Apr 27, 2015 at 09:58 PM

If you press on one of your buttons, there are a variety of options. One of them is "Transition". You can change the mode from tint (tints the button to whatever colour you want), Sprite swap (allows you to use your images) and animation. Once in any of those modes,just change individual states below it. Good luck.

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 3D - Rotating an object in relation to its velocity 0 Answers

Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers

Get user's name in Unity using GUILayout.TextField() 2 Answers

How to disable the runtime GUI on the Network Manager Hud when you connect to a server?,How do you disable the runtime GUI on the Network Manager Hud when connected to a server 0 Answers

Mouse and Oculus Touch Controller Inputs 0 Answers


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