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
0
Question by ReNo · Jun 03, 2010 at 01:39 PM · arraymaterialchange

How to change a material in Unity

Hello Everybody! I am trying to create a script which would allow me to change a material in realtime. I mean: I have a wall with a wooden material (for example called woody), and I want that when people clicks on it, it will change to another material (for example a "grassy" one). I tried various scripts, but any helped me...

I now I (maybe) have to create an array, but I am not a good programmer atm.

I hope someone can help me and thanks in advance.

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
4

Answer by equalsequals · Jun 03, 2010 at 01:48 PM

The simple way to implement this is to create a public reference to a Texture2D and assign it the texture you want in the Inspector.

public var myNewTexture2D:Texture2D;

Putting this in your script will allow you to click and drag or select a new texture from the drop down list in your Inspector Panel.

From there when you want to change your texture you just have a reference to the Material you want to swap textures of and assign the new texture using this:

renderer.material.mainTexture = myNewTexture2D; 

This sets whatever the material's "_MainTex" (usually the diffuse texture)

You can also use the Material.SetTexture() method:

renderer.material.SetTexture("_BumpMap", myNewTexture2D);

There really isn't a lot of code other than that. These lines of code are assuming that the script they are implemented in are attached to the game object that has the material you are intending to change.

Cheers,

==

Comment
Add comment · Show 2 · 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 ReNo · Jun 03, 2010 at 02:00 PM 0
Share

Thanks for the help, but can you show me a pseudo-complete script, so I can have an idea on how to use this?

Thx a lot btw :)

avatar image equalsequals ReNo · Jun 03, 2010 at 02:05 PM 0
Share

If you want to respond to someone's answer, leave a comment, not another answer.

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

1 Person is following this question.

avatar image

Related Questions

Load, change material to game object II (not a repeat) 1 Answer

Plane - 10x10 grid - Questions about it. 1 Answer

Spawn object with different material. 0 Answers

How do i get texture from GUI box? 1 Answer

Hair Color 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