- Home /
How to create a GUI button to change a character's texture in real-time?
Hi, I'm trying to have a button on my screen that will change the texture of my character if the user presses that button. the material with the texture i want to swap is on my gameObject called "joint1".
I don't really know what I'm doing... artist trying to do whatever sounds basic in a game-engine...
This is my code at the moment:
 var material1 : Material;
  
 
 function Update () {
 
     for (GUI.Button(Rect(Screen.width - 74,148,64,64)))
     {
         GameObject.Find("joint1").renderer.sharedMaterial = material1;
     }
 }
I decided to make it a toggle with an icon ins$$anonymous$$d... now if I could just work out how to get rid of that stupid... toggle dot...
Answer by reganmusic · Nov 30, 2012 at 12:58 AM
Here's the script I came up with eventually. I didn't know how to swap the textures so... MAJOR THANK YOU!!! I will get to doing it that way tomorrow to save on materials.
I decided to go with a toggle instead of a button.
=============================================================
// Make the script also execute in edit mode @script ExecuteInEditMode()
 var matIcon : Texture2D;
 var material1 : Material;
 var material2 : Material;
 var recPosX = 1;
 var recPosY = 1;
 
 var toggleBool = true;
 
 function OnGUI () {
     var toggleBoolNew = GUI.Toggle (Rect ( recPosX , recPosY , 100, 64), toggleBool, GUIContent (matIcon, "Toggle"));
 
     // Check if the toggle was toggled
     if (toggleBoolNew != toggleBool) {
         if (toggleBoolNew == true)
             GameObject.Find("joint1").renderer.sharedMaterial = material1;
         else
             GameObject.Find("joint1").renderer.sharedMaterial = material2;
 
         toggleBool = toggleBoolNew;
     }
 }
 
Glad I could help, please mark it as answered if your satisfied with the answer .. thanks.
Answer by Griffo · Nov 29, 2012 at 03:15 PM
Try this ..
 #pragma strict
 
 var myTexture : Texture2D;
 private var go : GameObject;
 
 function Start(){
 
     go = GameObject.Find("joint1");
 }
 
 function Update () {
 
     if (GUI.Button (Rect (20,40,80,20), "Texture")) {
         go.renderer.material.mainTexture = myTexture;
     }
 }
hey dude, thanks for reply. I managed to find a way to do it by swapping the material, so your way is different again swapping the texture... that's more what I was after, I will probably use your "go.renderer.material.mainTexture = myTexture;" sometime tomorrow, saves on using lots of different materials.
I need to sleep now, here's the code I came up with... i'll put it as an answer...
Your answer
 
 
             Follow this Question
Related Questions
Changing GameObject texture? 4 Answers
Offset detail texture in c# 1 Answer
How do I get Texture from material 1 Answer
How can I combine GUI button with script that is put on different gam objects? 1 Answer
Render GUI with Material 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                