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 ahmetabaci · Apr 20, 2016 at 05:11 PM · buttonmaterialtogglebutton trigger eventsmaterial color

How to change multiple objects' material (color) with one button?

I've searched most of the community questions and answers related to this topic and also youtube tutorials. But I couldn't find my answer. Here is the problem;

  • have 3 cubes with different colors.

  • want to add a button that will change all of their colors in the first click. Then, in the second click, colors will return to their first state. It can be toggle, too.

If there is a youtube tutorial or something else, I'm eager to see them also.

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 Naphier · Apr 21, 2016 at 10:18 AM 0
Share

Also note that you can declare public Renderer cubeRenderer and avoid the call to GetComponent. And you can make your life simpler with arrays i.e. public Renderer[] cubeRenderers. Thus assigning multiple in the editor and being able to iterate through them with a for loop. And do yourself a favor and use a bool for flags ins$$anonymous$$d of an int like you're doing in Change().

avatar image Ali-hatem Naphier · Apr 21, 2016 at 02:26 PM 0
Share

@Naphier i have already mentioned

so i tried to make it lees code with loops but since you want different colors i did it like this

because in loop how you change each object with different colors .

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ali-hatem · Apr 20, 2016 at 07:38 PM

so i tried to make it lees code with loops but since you want different colors i did it like this

 int counter = 0;
 public GameObject Cube1;
 public GameObject Cube2;
 public GameObject Cube3;
 Renderer rn1;
 Renderer rn2;
 Renderer rn3;
 Color  col1;
 Color  col2;
 Color  col3;

 void Start(){
     rn1 = Cube1.GetComponent<Renderer>();
     rn2 = Cube2.GetComponent<Renderer>();
     rn3 = Cube3.GetComponent<Renderer>();
     col1 = rn1.material.color;
     col2 = rn2.material.color;
     col3 = rn3.material.color;
 }
 public void Change(){
     if (counter == 0) {
         newColor ();
         counter = 1;
     } else {
         oldColor ();
         counter = 0;
     }
 }
 void oldColor(){
     rn1.material.color = col1;
     rn2.material.color = col2;
     rn3.material.color = col3;
 }
 void newColor(){
     rn1.material.color = Color.gray;
     rn2.material.color = Color.green;
     rn3.material.color = Color.black;
 }

also i am getting weird warning "UnassignedReferenceException : The variable Cube1 of Control has not been assigned." even after assigning all cubes in inspector but every thing work fine i hop it's a bug .

Comment
Add comment · Show 4 · 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 ahmetabaci · Apr 21, 2016 at 07:36 AM 0
Share

Thank you very much. Code is working. Can I ask you something else? I want to place and fix my button to somewhere in the 3D scene. But it moves relatively with the character when I press the play button. How can I fix this?

avatar image RandomDucks ahmetabaci · Apr 21, 2016 at 07:42 AM 0
Share

Have you tried setting the canvas to not scale with the view? this should make it work.

avatar image ahmetabaci RandomDucks · Apr 21, 2016 at 08:09 AM 1
Share

I changed the canvas' render mode to World Space and problem is solved. Thanks

Show more comments

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

7 People are following this question.

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

Related Questions

Toggle material color and shader 1 Answer

Car Model Run time Color Change Problem 0 Answers

How to change the material of a prefab in script? 0 Answers

Is there a way to move a button to a random location after a certain number of clicks? 1 Answer

Best Practices for buttons? 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