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 8r3nd4n · Jan 22, 2015 at 04:59 AM · texturematerialsrendererinstance

Change the texture on a material at runtime without creating an instance

I have a few objects in my scene that the user can change the main colour of the material via a colour picker script.

The Object (a ball) has 4 materials on it since the user can customise the balls colours and each of these materials except for the base is a transparant material. At first everything works as it should. The user picks a colour from the picker and it changes the colour of the material. So far so good.

Next I want to incorporate a new design. Rather than duplicating everything and have a new mesh with new materials (increasing draw counts and overheads), I thought I would just change the texture on the ball objects material. This would also mean that the colours the user had previously chosen would carry over to the new design.

However, as soon as I push the button to change the design, the materials on the ball object become instances and the colour picker no longer functions. I don't know why when I try to change the texture it changes the material to an instance of the original one.

Is there something/some concept I'm missing?

 using UnityEngine;
 using System.Collections;
 
 public class BallSelector : MonoBehaviour {
 
     public Texture[] hurricane;
     public Texture[] cyclone;
     string currentStyle;
 
     void OnGUI()
     {
         if(GUI.Button(new Rect(Screen.width - 120, 20, 100, 50),"Hurricane"))
         {
             currentStyle = "Hurricane";
             ChangeStyle(currentStyle);
         }
         if(GUI.Button(new Rect(Screen.width - 120, 80, 100, 50),"Cyclone"))
         {
             currentStyle = "Cyclone";
             ChangeStyle(currentStyle);
         }
         
     }
 
     void ChangeStyle(string style)
     {
         switch(style){
         case "Hurricane":
             renderer.materials[0].mainTexture = hurricane[0];
             renderer.materials[1].mainTexture = hurricane[1];
             renderer.materials[2].mainTexture = hurricane[2];
             renderer.materials[3].mainTexture = hurricane[3];
             break;
         case "Cyclone":
             renderer.materials[0].SetTexture("_MainTexture",cyclone[0]);    //tried something else but same result
             renderer.materials[1].mainTexture = cyclone[1];
             renderer.materials[2].mainTexture = cyclone[2];
             renderer.materials[3].mainTexture = cyclone[3];
             break;
         }
     }
 }
 
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
Best Answer

Answer by AndresBarrera · Jan 22, 2015 at 09:07 AM

Try using renderer.sharedMaterial. But this will modify the material for all your objects using it.

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 8r3nd4n · Jan 22, 2015 at 10:27 AM 0
Share

You sir are a legend. I knew it was probably something so simple.

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

26 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 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

Changing scrolling textures on a plane using transparency 1 Answer

So, what's the problem with this code? 1 Answer

Having a problem with the second material (on the same renderer) 0 Answers

How to reload a mainTexture to a mesh? 1 Answer

How to check if a Renderer has instantiated materials without instantiating them? 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