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
1
Question by reganmusic · Nov 29, 2012 at 02:44 PM · guitexturematerialcharacterchange

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;
     }
 }
Comment
Add comment · Show 1
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 reganmusic · Nov 29, 2012 at 03:33 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
 
Comment
Add comment · Show 2 · 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 Griffo · Nov 30, 2012 at 11:21 AM 0
Share

Glad I could help, please mark it as answered if your satisfied with the answer .. thanks.

avatar image reganmusic · Dec 04, 2012 at 11:45 PM 0
Share

hi, not sure how to mark as answered

avatar image
0

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;
     }
 }
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 reganmusic · Nov 29, 2012 at 03:31 PM 0
Share

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

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

11 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

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


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