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 MariusVE · Jan 18, 2013 at 04:24 PM · materialsetcolor

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!

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

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!

Comment
Add comment · Show 1 · 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 MariusVE · Jan 18, 2013 at 06:39 PM 0
Share

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

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

10 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

Related Questions

2D Sprite Texture Animation - SetTexture or SetTextureOffset 1 Answer

Is it possiable to change the Emissive Color on VertexLit Blended Material with a script? 3 Answers

Setting color information and passing it in scenes 1 Answer

setColor function not working as intended 1 Answer

SetTexture on Quad black (but only on one camera) 0 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