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 /
avatar image
0
Question by oliviervet · Jun 22, 2016 at 05:13 AM · materialsdropdown

how to change multiple textures in one click?

Hi I am just learning Unity and get stuck with this.

I am building a city simulator that is build up of simple 3d blocks with a texture. To show different scenarios I have multiple textures for each building, IE img_1 is poor and img_2 is rich : (in which img is the imagename)

What I want is on the canvas I have a drop down box: Setting 2 options : Rich and Poor, so once I select one option all the textures will change.

So 2 things - how to change material using C# and How to call that with a standarddrop down menu (unity5)

I watched lots of videos but still cannot figure it out... Can someone give me directions?

Thanks so much, all help is highly appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dibbie · Jun 22, 2016 at 07:34 AM

Take a look at Unitys Dropdown menu guide, it may give you a good understanding of how it works.

To achieve what you want, you need an array of GameObjects.

Your array would store all of the Game Objects, in your case your cubed buildings you want to update on the button press.

Now its best to create this script attached to your camera or some object thats easily accessible and is never destroyed. Then creating 2 public void functions, one for the "poor switch" and one for the "rich switch", which would be attached to your Dropdown's OnClick event in the Inspector.

Once that is done, you need your "poor" and "rich" textures... Depending on if it is 1 universal/generic "poor' and "rich" then creating 2 public Textures will work... But if every building has a different "poor" and "rich" texture, then you need to make a second script, that has public Textures for both that buildings specific "poor" and "rich".

So if its generic, your script to actually update every building would look something like:

 public void SwitchBuildingsTextures(Texture img){
 //Get every single building from your array
 for(int i = 0; i < yourArrayOfGameObjects.Length - 1; i++){
         //access each buildings Materal, so you can affect its Texture
         yourArrayOfGameObjects[i].GetComponent<Renderer>().Material.Texture = img;
     }
 }

And your 2 public "MakeBuildingPoor" and "MakeBuildingRich" functions that your dropdown is looking at, is what would call "SwitchBuildingsTextures", and pass in the public reference to your generic rich/poor texture.

But if its not generic, then you would have to change that "SwitchBuildingsTexures" to something like:

 public void SwitchBuildingsTextures(bool poor){
 //Create a temp variable for the texture
 Texture img;
 
     //Get every single building from your array
     for(int i = 0; i < yourArrayOfGameObjects.Length - 1; i++){
         //Set the texture
 if(poor == true){
         img = yourArrayOfGameObjects[i].GetComponent<YourSecondScript>().poorTexture;}
 else{             img = yourArrayOfGameObjects[i].GetComponent<YourSecondScript>().richTexture;}
 
 //access each buildings Materal, so you can affect its Texture
             yourArrayOfGameObjects[i].GetComponent<Renderer>().Material.Texture = img;
         }
     }

This would probably be the easiest and quickest solution I can think of right now.

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

Answer by oliviervet · Jun 22, 2016 at 07:40 AM

@dibbie

Thanks a lot, I'm going to dive in it...

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

59 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

Related Questions

Purple textures problem 0 Answers

Inexplicable loss of frame rate 3 Answers

2 variables holding Renderer.materials change at the same time... 0 Answers

Set an Object's 2d physics material in c# 0 Answers

Enabling multiple materials on canvas renderer 2 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