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 /
avatar image
1
Question by EncryptedCow · Apr 23, 2013 at 03:55 AM · javascriptmaterialvariableassignment

Changing material depending on variable

I have an object that I am trying to change the material depending on a user decided variable. I am trying to change what material a variable references during runtime (eg., player sets the variable to 3 and it will find the Material named 3 and assign it to flagMat. Is there any way to do this? I don't really want to type out nearly 200 if statements.

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
2
Best Answer

Answer by robertbu · Apr 23, 2013 at 04:45 AM

An easy way to deal with a list of materials is to use an array. Here is a sample script that uses an array of materials and randomly changes the material each time the space bar is pressed:

 #pragma strict
 
 var materials : Material[];
 
 function Update () {
 
     if (Input.GetKeyDown(KeyCode.Space)) {
         renderer.material = materials[Random.Range(0,materials.Length)];    
     }
 }

You would initialize materials in the inspector. Click on the arrow next to the 'materials' variable, set the size of the array then drag and drop the materials into the array. You can instead load the materials array using Resources.Load().

Note given that you are going to have 200 materials, I have to wonder how the materials differ. If it is only a texture or color, it might be easier/better to keep a single material and just set a new texture/color for the material.

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 EncryptedCow · Apr 23, 2013 at 05:08 AM 0
Share

It's a cloth that will have every countries flag, so I can't just change the colour unfortunately. But I guess if I just have to assign each flag in the inspector thats better than a bunch of ifs. I have another variable flagNum that gets changed by the player (pressing [ subtracts 1, pressing ] adds 1) and I have the flag materials named 1, 2, 3, etc. I was hoping there was some way that I could first assign flag$$anonymous$$at to a material depending on that variable, then set renderer.material to flag$$anonymous$$at.

avatar image Eric5h5 · Apr 23, 2013 at 05:49 AM 2
Share

You can select all the materials and drag them onto the materials array at once (use the lock icon in the inspector). It doesn't matter what they are named; you just use a number to refer to them in the order they are listed in the array. So it would be better to use more descriptive names ins$$anonymous$$d of "1", "2", etc.

avatar image robertbu · Apr 23, 2013 at 11:24 AM 0
Share

If all that changes is the texture (bitmap), then you don't need to change the material for each flag...just change the texture. You can create a texture array similar to the materials array in the example above, or you could dynamically load the flag you want Resources.

avatar image EncryptedCow · Apr 23, 2013 at 02:56 PM 0
Share

Okay I thought I would have to add each flag individually to the array. But I guess if I can drag them all at once, that's perfect! And robertbu, I'm using materials not textures.

avatar image
2

Answer by Bunny83 · Apr 23, 2013 at 03:15 PM

Another way which is usually easier is to use one flag atlas like this one and adjust the uv coords of your mesh. However in any case you have to define in which order they are so you can find the right one.

If you use an array like robertbu mentioned, add them in this order: SystemLanguage. That way you can use Application.systemLanguage directly as index ;)

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

13 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

Related Questions

When i call some variables to an other second script i have a problem 1 Answer

converting javascript to c# 1 Answer

What is the problem with this code? 1 Answer

Need help with GUI label not displaying variable 1 Answer

Trying to proceed to next level when multiple items have collided 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