- Home /
After material.SetColor, SetTexture is ignored
Hi All,
I'm new with Unity3D and I have a question:
I would like to be able to "highlight" an object in my scene when the mouse hovers on top of it and then unselect it when the mouse leaves it. After that, I would like to change the texture of the material that is used by this object.
I wrote a script for highlighting/unhighlighting the object such as the following:
void Start()
{
startColor = renderer.material.GetColor("_Color");
}
void OnMouseEnter()
{
renderer.material.SetColor("_Color", Color.yellow);
}
void OnMouseExit()
{
renderer.material.SetColor("_Color", startColor);
}
I have assigned this script to the object and everything works fine.
This object uses a material that has a texture assigned to it. The material is used by some other objects in the scene as well (also able to be highlighted/unhighlited).
The problem is that I have another script that, when the user presses a GUI button, changes the texture of this material like this:
woodMaterial.SetTexture("_MainTex", woodTexture"); // where woodMaterial is of type Material and woodTexture is the texture that I want to assign to the material.
This script is assigned to the Camera that overviews the objects in the scene.
If I highlight the object, highlighting works OK but when pressing the button to change the texture, the texture is not changed.
If I comment out the lines that manipulate the color in the highlight script, the texture is set successfully when pressing the GUI button for changing the texture.
The material is assigned through the Inspector and I am using Unity3d v4.0
Any ideas what is going on ?
I noticed that even the first line "startColor = renderer.material.GetColor("_Color"); " causes the SetTexture to not work anymore.
Thanks for any hints or help!
Answer by C.Malcolm · Jan 18, 2013 at 06:02 PM
Hi there! Just a quick thing which might just be a typo but just in case this is the problem, this line should not have the quotation mark after woodTexture:
woodMaterial.SetTexture("_MainTex", woodTexture"<-----this one here);
The other thing is that I have managed to apply the texture successfully in Unity using the code supplied however the only difference is that I have everything in one script whereas you applied the script to the Camera. Applying the script to the camera should work as long as you have created a variable in the script for the game object you want it to affect. Here is my code which does work:
public Color startColor;
public Texture2D woodTexture;
// Use this for initialization
void Start () {
startColor = renderer.material.GetColor("_Color");
}
void OnMouseEnter()
{
renderer.material.SetColor("_Color", Color.yellow);
}
void OnMouseExit()
{
renderer.material.SetColor("_Color", startColor);
}
void OnGUI(){
if(GUI.Button(new Rect(10,10,150,100), "click meeee")){
renderer.sharedMaterial.SetTexture("_MainTex", woodTexture);
}
}
I hope this helps, if you want any more help, I'll see what I can do!
Hi,
By the time my question was approved, I managed to solve the problem as well. :) Indeed, I found out about the difference between shared$$anonymous$$aterial and "material"; I didn't expect that accessing the material property of the renderer would actually create a COPY of the material and so accessing the shared material via the other method would have no effect on the "copy of" the material that would be assigned to the object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091143im_/https://answers.unity.com/themes/thub/images/avi.jpg)