Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 MSW-Games · Jul 07, 2015 at 10:05 PM · c#scripting probleminputarrayrenderer

Trying to change character skins at the push of a button. C#

I'm trying to set up a skin selector for my character. I want to be able to set up a manual method for selecting between multiple skins (i.e. a hotkey, a GUI Button, etc.) For some reason, I just can't figure it out. I've been trying for about an hour and can't seem to nail it down. Here's what I have. I'm sure it's a simple answer.

Note: I started with the Renderer.material script in the API

Sorry, I'm doing a terrible job of explaining it.

I am loading materials up in the Materials[] in the inspector. At the start of the game, I want players to be able to choose the skin they want and have that persist throughout the game.

I'm trying to find an efficient way to allow players to select the skin they want using a GetButton command. I'm having a hard time using a get button command to cycle between the materials loaded in the inspector.

 public class SkinSelect : MonoBehaviour {
     public Material[] materials;
 
     public Renderer rend;
     public GameObject player; 
     public int skin;
 
     
     void Start() {
         skin = 0;
         player = GameObject.FindGameObjectWithTag ("Player");
         rend = player.GetComponent<Renderer>();
         rend.enabled = true;
     }
     
     void Update() {
         if (Input.GetButton ("C")) {
 
             *What goes here?*
 
 
         }
         if (materials.Length == 0)
             return;
     }
 }


Right now, I'm using the script below. It works, but I feel like it's really sloppy.

 public class SkinSelect : MonoBehaviour {
     public Material material1;
     public Material material2;
     public Material material3;
     public Material material4;
     public Material material5;
 
     public Renderer rend;
     public GameObject player; 
     public int skin;
 
     
     void Start() {
 
         skin = 1;
 
         player = GameObject.FindGameObjectWithTag ("Player");
         rend = player.GetComponent<Renderer>();
         rend.enabled = true;
 
     }
     
     
     void Update() {
 
 
         if (Input.GetButtonDown ("C")) {
             skin = skin +1;
             ChangeSkin();
             }
         if (skin >= 6) {
             skin = 1;
             ChangeSkin ();
         }
         DontDestroyOnLoad (this);
 
     }
 
     void ChangeSkin()
     {
         if (skin == 1) 
         {
             rend.material = material1;
         }
         if (skin == 2) 
         {
             rend.material = material2;
         }
         if (skin == 3) 
         {
             rend.material = material3;
         }
         if (skin == 4) 
         {
             rend.material = material4;
         }
         if (skin == 5) 
         {
             rend.material = material5;
         }
 
 
     }
 }
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
1

Answer by Pacosmico · Jul 08, 2015 at 01:05 AM

Your question is a little confusing to me, sorry, but I think you want somthing like this:

 if (Input.GetButton ("C") && materials.Length > 0) {
 
   skin++;
   if (skin >= materials.Length) skin = 0;// To cycle, you can also do
   // skin = (skin+1) % materials.Length;
 
   rend.sharedMaterial = materials[skin]; // Use shared material if you don't want to go on instantiating them
 
 }
 
 //also i moved your check for material count up, because no sense having it here after everything happened

PD: sorry for bad grammar, I'm not native english speaker PD2 : on the "Start" you don't need to find the Player if you are going to have it being seteable from inspector (you know the "public" part of "public GameObject")

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 MSW-Games · Jul 08, 2015 at 01:14 AM 0
Share

Sorry, I'm doing a terrible job of explaining it.

I am loading materials up in the $$anonymous$$aterials[] in the inspector. At the start of the game, I want players to be able to choose the skin they want and have that persist throughout the game.

I'm trying to find an efficient way to allow players to select the skin they want using a GetButton command. I'm having a hard time using a get button command to cycle between the materials loaded in the inspector.

avatar image Pacosmico · Jul 08, 2015 at 04:53 PM 0
Share

well, try that... but.. as dcaamano pointed out, maybe it's better change "GetButton" for "GetButtonDown" so it doesn't go crazy

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

23 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

Related Questions

Distribute terrain in zones 3 Answers

Mesh Renderer Matereal Aray reduce? 0 Answers

How can I add a MeshRenderer component with all properties to a GameObject by script ? 1 Answer

Renderer on object disabled after level reload 1 Answer

New Input System Local Multiplayer Help 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