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.
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:
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.
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 :)
If you want to respond to someone's answer, leave a comment, not another answer.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
1 Person is following this question.
A node in a childnode?
Plane - 10x10 grid - Questions about it.
Load, change material to game object II (not a repeat)
How do i get texture from GUI box?