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 /
This question was closed Sep 03, 2013 at 05:07 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AracosDE · Sep 03, 2013 at 01:15 AM · arraymaterialsshared materials

Switching all Materials on objects using the same.

Newbie Question I guess but here goes:

GOAL:

Part 1: If a switch is activated switch Material 1 (Black) to Material 2 (White) and vice versa.

Part 2: Make sure this works for all objects the script is attached to.

So far it works for exactly 1 object in the scene e.g. Part 1 works, Part 2 doesn't.

I've the feeling I'm missing the obvious answer but after trying my luck for 3? hours I give up.

Anyway here's the code snippet.

 {
     public Material BlackMat;
     public Material WhiteMat;
     public Material PermanentMat;
 
     public static bool switchActivated= false;
 
     private string Material;
 
 
     void Update () {
 
             Material = renderer.materials[0].name;
 
 
             if (switchActivated == true && Material == "White (Instance)")
             {
 
                 renderer.material = BlackMat;
                 switchActivated = false;
             }
             else if (switchActivated== true && Material == "Black (Instance)")
             {
                renderer.material = WhiteMat;
                 switchActivated = false;
             }
     }
 }
Comment
Add comment · Show 6
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 Tanshaydar · Sep 03, 2013 at 10:16 AM 0
Share

You never switch switchActivated back to true?

avatar image AracosDE · Sep 03, 2013 at 10:51 AM 0
Share

I do switch switchActivated to true only when the switch is being used. Which is being controlled from a different script.

avatar image Tanshaydar · Sep 03, 2013 at 10:56 AM 0
Share

Well, first you need to change your string variable's name because $$anonymous$$aterial is a keyword for Unity. As a matter of fact you should get an error with that. Secondly, you can change your material with

 $$anonymous$$aterial White$$anonymous$$
 $$anonymous$$aterial Black$$anonymous$$
 gameObject.renderer.material = White$$anonymous$$; //(or Black$$anonymous$$)

using editors inspector to choose materials from file inspector.

avatar image AracosDE · Sep 03, 2013 at 11:44 AM 0
Share

$$anonymous$$aterial with an uppercase "$$anonymous$$" is only used as a declaration and can therefor be used as a variable name for a string. Though it's not exactly the "cleanest" way of coding... I blame late night coding fatigue for that ;)

Guess I need to clarify what's wrong in my original question: The script works as "intended" for 1 object in the scene. If the same object is duplicated a bunch of times it will still only work on the original one. I don't understand why this script will only work on one object and not on all the objects it is attached to.

avatar image Tanshaydar · Sep 03, 2013 at 12:16 PM 0
Share

I see. You might wanna keep the track of the objects in the scene as a list and go over them one by one like objects[0/1/2..].renderer.material or make a prefab and attach the script over it so every single object will work for on its own.

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by AracosDE · Sep 03, 2013 at 05:04 PM

Yeah so in case anyone else runs into the same issue:

"switchactivated == false" is being set immediately after the first material gets changed therefor all the others won't.

Guess this problem should be filed under: Wasting time by missing the obvious.

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

18 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

Related Questions

Changing the material of all children, then change back to old one 1 Answer

Changing the material in an array of materials at run time 2 Answers

How can I use the same script on multiple objects without conflicting variables. 2 Answers

array with a list of materials 3 Answers

How to assign array to renderer.Materials 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