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 /
avatar image
0
Question by BunnyTales · Oct 26, 2017 at 09:03 PM · colorcolor changesetcolorgetcolor

Get color of button and set to an object

I am new to scripting and need some help. What I have on my scene is several buttons of many different colors. I also have an object with a material on it. How would I go about making the albedo color of my object be set to the color of the button that is pressed? (I would like it so that if I pressed the red button the object would change to red) Is there a way that I could get the color of the button that got pressed and then set that color to be the albedo of my object? I don't know exactly how to write it but this is how I picture it would look in scripting: objectAlbedoColor=getColor(button);

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

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

Answer by zachwuzhere · Oct 26, 2017 at 11:51 PM

something like this?

 Button mybutton;
 GameObject myGameObject;

 void MyButtonClickFunction(){
      //Get the color from you buttons material component
      Vector4 myButtonsColor = mybutton.GetComponent<Material>().color;
      /*Get the material component from your game object 
      and set its color to the new color defined above*/
      myGameObject.GetComponent<Material>().color = myButtonsColor;
 }
 







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 BunnyTales · Oct 27, 2017 at 12:43 AM 0
Share

I got it set up but I am getting an error: $$anonymous$$issingComponentException: There is no $$anonymous$$aterial attached to the "Button" game object but a script is trying to access it. The color of the button is in the Image section of the button. (Right under source image). I do not have a material assigned to the button. Thanks for your help.

avatar image zachwuzhere BunnyTales · Oct 27, 2017 at 01:10 AM 0
Share

If you need to get the color from the image of your button then try this... Vector4 myButtonsColor = mybutton.image.color;

avatar image BunnyTales zachwuzhere · Oct 27, 2017 at 02:16 AM 0
Share

I got it working! YAY! Here is the code with some edits. (because for some reason it told me that my object didn't have a material even though it clearly did) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ButtonColor : $$anonymous$$onoBehaviour {

 public Button mybutton;
 public GameObject myGameObject;

 public void $$anonymous$$yButtonClickFunction()
 {
        //Get the color from you buttons material component
   Vector4 myButtonsColor = mybutton.image.color;
     /*Get the material component from your game object 
     and set its color to the new color defined above*/
     myGameObject.GetComponent<Renderer>().material.SetColor("_Color", myButtonsColor);

     }

}

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

72 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Image UI color not changing when sprite is set via script 0 Answers

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

Color of some instantiated objects differs from the normal ones. 1 Answer

Changing the color of individual particles 0 Answers

Button image sprite colour change 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