Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Oct 25, 2015 at 07:21 AM by HenkPinkster for the following reason:

Other

avatar image
0
Question by HenkPinkster · Oct 23, 2015 at 08:49 PM · color

Change color from script

I'm making an app for school with a question and 3 answers. 1 answer is good and the other 2 are false. Now i want to change the color of the button to green if it's good and to red if it's false. I want to do this from the inspector, but i don't know how to apply this to the button.

This is my script.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class LevelManager : MonoBehaviour {
     // answers
     public string Question;
     public string answer1;
     public string answer2;
     public string answer3;
 
     // colors
     public Color FalseAnswerColor;
     public Color GoodAnswerColor;
  
     // get the text elements
     public UnityEngine.UI.Text Questiontext;
     public UnityEngine.UI.Text Answertext1;
     public UnityEngine.UI.Text Answertext2;
     public UnityEngine.UI.Text Answertext3;
 
     // get the button elements
     public UnityEngine.UI.Button AnswerButton1;
     public UnityEngine.UI.Button AnswerButton2;
     public UnityEngine.UI.Button AnswerButton3;
 
     void Start ()
     {
         Questiontext.text = Question;
         Answertext1.text = answer1;
         Answertext2.text = answer2;
         Answertext3.text = answer3;
     }
 
     /*
     // The answer is true
     public void RightAnswer ()
     {
         // reward animation
         // Create the reward animation and implement it
         // change the color to green
 
     }
     */
 
     // The Answer is false
     public void FalseAnswer ()
     {
         // change the color the red
 
     }
 }
 
Comment
Add comment
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
1
Best Answer

Answer by SniperEvan · Oct 24, 2015 at 06:31 AM

  1. Get a reference to the button you are changing. (Either thru the click event or by passing it to the method as a parameter somehow)

  2. Set the color of the button. Unity uses color blocks for buttons so that we can have pressed color, disabled color, etc.

Psuedocode:

 ColorBlock cb = button.colors;
 
 If (answer == wrong)
  cb.normalColor = FalseAnswercolor
 else
  cb.normalcolor = goodanswercolor
 
  b.colors = cb; (assign the color block to the button)
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

Follow this Question

Answers Answers and Comments

31 People are following this question.

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

Related Questions

SpriteRenderer.Color not adjustable with both mecanim + script 1 Answer

Texture color completely off 0 Answers

change color 2 Answers

How to Change the Color of Text without using Markup Text? 0 Answers

Need help to make a color replace shader 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