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 potter3366 · Dec 20, 2012 at 04:01 PM · textures

Change textures at runtime on object with multiple textures?


I have a cube with 3 textures (green, black and blue). I'm trying to make it so the original textures (green, black and blue) on my mesh (cube) change every 5 seconds. Only lateral textures will be changed (green to yellow, blue to red). After 2 seconds yellow and red textures will be changed again to original texures. I have a problem when new textures come back to original.. The problem is probably in "instanced" materials.. I have noticed in Inspector that one material just disappeas. Btw Swap routine works fine.

 public class Change: MonoBehaviour
 {
    
     public float coldTimer = 5.0f;
     public float warmTimer = 2.0f;
     public bool warming;
    
     public Transform myCharacter;
     public Texture oldTexturev1;
     public Texture newTexturev1;
     public Texture oldTexturev3;
     public Texture newTexturev3;
    
     private Material[] sharedMaterials;
     private Material[] myMaterials;
     private Material[] myMaterials1;
     private Material tmp;
    
     private float currTime;
    
     void Start()
     {   
         currTime = coldTimer;
         warming = false;  
        
         myMaterials = myCharacter.renderer.materials;
         myMaterials1 = myCharacter.renderer.materials;
         //Debug.Log(myMaterials[0]);
         //Debug.Log(myMaterials[2]);   
     }
    
     void Update()
     {   
         if(currTime > 0.0f)
         {
             currTime -= Time.deltaTime;
         }
         else
         {
             if(warming)
             {
                 currTime = coldTimer;
            
                 tmp = myMaterials[0];
                 myCharacter.renderer.material = tmp;
                 myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev1);
            
                 tmp = myMaterials[2];
                 myCharacter.renderer.material = tmp;
                 myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev3);
                
                 warming = false;               
             }
             else
             {
                 currTime = warmTimer;
                
                 tmp = myMaterials[0];
                 myCharacter.renderer.material = tmp;
                 myCharacter.renderer.material.SetTexture("_MainTex", newTexturev1);
                
                 tmp = myMaterials[2];
                 myCharacter.renderer.material = tmp;
                 myCharacter.renderer.material.SetTexture("_MainTex", newTexturev3);
                
                 myCharacter.renderer.materials = myMaterials1;
                
                 warming = true;
                 //Swap();
             }          
         }   
        
     }
    
     void Swap() {
         tmp = myMaterials[0];
         myMaterials[0] = myMaterials[2];
         myMaterials[2] = tmp;
         myCharacter.renderer.materials = myMaterials;
     }
    
 }

 

Download package here: http://forum.unity3d.com/threads/163146-Change-textures-at-runtime-on-object-with-multiple-textures

Comment
Add comment · Show 2
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 potter3366 · Dec 21, 2012 at 07:58 AM 0
Share

There is no problem if object has just one material..

avatar image PushpaK · Dec 21, 2012 at 12:31 PM 0
Share

What do you want actually?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by potter3366 · Dec 21, 2012 at 09:45 PM

SOLVED!

using UnityEngine; using System.Collections;

public class Change: MonoBehaviour {

 public float coldTimer = 5.0f;
 public float warmTimer = 2.0f;
 public bool warming;

 public Transform myCharacter;
 public Texture oldTexturev1;
 public Texture newTexturev1;
 public Texture oldTexturev3;
 public Texture newTexturev3;

 private Material[] myMaterials;
 private Material[] myMaterials1;

 private float currTime;

 void Start() 
 {   
     currTime = coldTimer;
     warming = false;

     myMaterials1 = myCharacter.renderer.materials;
     myMaterials = myMaterials1;
     myCharacter.renderer.materials = myMaterials;   
 }

 void Update()
 {   
     if(currTime > 0.0f)
     {
         currTime -= Time.deltaTime;
     }
     else
     {
         if(warming)
         {
             currTime = coldTimer;

             myCharacter.renderer.material = myMaterials[0];
             myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev1);
             myCharacter.renderer.materials = myMaterials;

             myCharacter.renderer.material = myMaterials[2];
             myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev3);
             myCharacter.renderer.materials = myMaterials;

             warming = false;                
         }
         else
         {
             currTime = warmTimer;

             myCharacter.renderer.material = myMaterials[0];
             myCharacter.renderer.material.SetTexture("_MainTex", newTexturev1);
             myCharacter.renderer.materials = myMaterials;

             myCharacter.renderer.material = myMaterials[2];
             myCharacter.renderer.material.SetTexture("_MainTex", newTexturev3);
             myCharacter.renderer.materials = myMaterials;

             warming = true;
         }           
     }   

 }


}

Comment
Add comment · 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

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

Getting texture from project files and placing in script 1 Answer

Sims like building 1 Answer

Is it possible to automate material creation with new textures? 2 Answers

How could I have a ball chooser menu? 1 Answer

Movie Texure Not Playing For Other People 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