- Home /
 
One script, multiple object texture change
Hi everyone!
My project needs a simple function. Some buttons on the screen, and by pressing them textures should change on multiple objects. Here is the script I found and modified a bit, and it needs some change so it won't be referenced to the object assigned to, but can find objects by Tag or any other means.
 var Texture1:Texture2D;
 var Texture2:Texture2D;
 var Texture3:Texture2D;
    
 function OnGUI () {
 
           if (GUI.Button (Rect (Screen.width/9, Screen.height/200, Screen.width/10, Screen.height/10), "Texture1")) 
    {
      
       if(Texture1)
       {
          Debug.Log("Texture1 Loaded Sucessfully...");
          GetComponent.<Renderer>().material.mainTexture = Texture1;
       }
           
       else
       {
          Debug.Log("Unable to Load Texture1...");
       }
      }
 
         if (GUI.Button (Rect (Screen.width/4.7, Screen.height/200, Screen.width/10, Screen.height/10), "Texture2"))
    {
      
       if(Texture2)
       {
          Debug.Log("Texture2 Loaded Sucessfully...");
          GetComponent.<Renderer>().material.mainTexture = Texture2;
       }
           
       else
       {
          Debug.Log("Unable to Load Texture2...");
       }
      }
 
          if (GUI.Button (Rect (Screen.width/3.18, Screen.height/200, Screen.width/10, Screen.height/10), "Texture3"))
    {
      
       if(Texture3)
       {
          Debug.Log("Texture3 Loaded Sucessfully...");
          GetComponent.<Renderer>().material.mainTexture = Texture3;
       }
           
       else
       {
          Debug.Log("Unable to Load Texture3...");
       }
      }
     }
     @script ExecuteInEditMode()
 
               Thank you in advance for your help.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Oribow · Apr 10, 2016 at 06:36 PM
Use direct references.
 var Texture1:Texture2D;
 var Texture2:Texture2D;
 var Texture3:Texture2D;
 var target1:GameObject;
 var target2:GameObject;
 //define as many targets as you like here
     
 function OnGUI () {
  
     if (GUI.Button (Rect (Screen.width/9, Screen.height/200, Screen.width/10, Screen.height/10), "Texture1")) 
     {
       
         if(Texture1)
         {
             Debug.Log("Texture1 Loaded Sucessfully...");
             target1.GetComponent.<Renderer>().material.mainTexture = Texture1; //Just add the target you want in front of the line
         }
            
         else
         {
             Debug.Log("Unable to Load Texture1...");
         }
     }
  
     if (GUI.Button (Rect (Screen.width/4.7, Screen.height/200, Screen.width/10, Screen.height/10), "Texture2"))
     {
       
         if(Texture2)
         {
             Debug.Log("Texture2 Loaded Sucessfully...");
             target1.GetComponent.<Renderer>().material.mainTexture = Texture2;
         }
            
         else
         {
             Debug.Log("Unable to Load Texture2...");
         }
     }
  
     if (GUI.Button (Rect (Screen.width/3.18, Screen.height/200, Screen.width/10, Screen.height/10), "Texture3"))
     {
       
         if(Texture3)
         {
             Debug.Log("Texture3 Loaded Sucessfully...");
             target1.GetComponent.<Renderer>().material.mainTexture = Texture3;
         }
            
         else
         {
             Debug.Log("Unable to Load Texture3...");
         }
     }
 }
      @script ExecuteInEditMode()
 
              Your answer