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 kushgodofhope · Feb 26, 2014 at 07:23 AM · javascriptmaterialcolorcustomization

how to change material color for player customization.

i am wondering how to change the color of a material in a character customization scene, and then load that material color onto the character into the scene with the game play.

my character is a bird and it has seperate materials for each body part (the body parts are child objects of the whole character), so the body has a material called "player_body", beak has material called "player_beak" and so on but for the purpose of the question ill just focus on the body material and apply the fix (if we find one, hopefully) to the rest.

i have a script that detects which body part you want to change color, and what color to set that material to. ill just leave the parts of the script related to the body. (game is for android)

  var PartOfBody; // sets what part of the body you want to change
       function Update ()
     {
       if (Input.touchCount > 0)
       {
           var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
           var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) 
       {
                if(hit.collider.gameObject.name == "txt_Body")
     {
     Debug.Log("hit the body text");
     PartOfBody = "Body";
     }
 
 
 // there are 3 colord boxes in my scene, if you touch one then  it changes the color of that object to either red,blue,green.
     if(hit.collider.gameObject.name == "Box_red")
     {
     Debug.Log("hit red box");
     GameObject.Find(PartOfBody).renderer.material.color = Color.red;
     }
     if(hit.collider.gameObject.name == "Box_blue")
     {
     Debug.Log("hit blue box");
     GameObject.Find(PartOfBody).renderer.material.color = Color.blue;
     }
         if(hit.collider.gameObject.name == "Box_green")
     {
     Debug.Log("hit green box");
     GameObject.Find(PartOfBody).renderer.material.color = Color.green;
     }
     
       }
 
      }
      }


however theres a few problems

  1. the color reverts after scene has stopped

  2. Apparently the material gets cloned so the color change wont affect any other objects wearing the same material ("Player_body").

so is there a way to make the color change permanent (through playerprefs perhaps? not sure how to use playerfres with a color), And/ormake it so every object with the same material has the new color aswell?

thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CodeElemental · Feb 26, 2014 at 08:32 AM

I think you should work with

 renderer.sharedMaterial 

instead of

 renderer.material.

Here's the documentation.

Comment
Add comment · Show 4 · 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 kushgodofhope · Feb 26, 2014 at 08:51 AM 0
Share

wow, such simple, so fix. very thanks

avatar image robertbu · Feb 26, 2014 at 08:57 AM 1
Share

In terms of modifying the original material, I believe this works in the editor, but will not modify the material in a build. I believe if you want to modify the material across executions, you will need to save and apply the settings on the next execution. 'shared$$anonymous$$aterials' does change all the objects that share that material.

avatar image kushgodofhope · Feb 26, 2014 at 09:18 AM 0
Share

sharedmaterial works fine because the only 2 objects that are going to be using the same material is the model of the character in character custimation scene, and the model of the character in the game scene

avatar image sidvangala26 kushgodofhope · Sep 04, 2018 at 09:55 PM 0
Share

I was trying the same.

It works only in the editor but not in build. Am I doing anything wrong ?

avatar image
0

Answer by Hakumaru · Feb 26, 2014 at 08:58 AM

You could try setting variables in the Player info files, and have another file read those variable and set the material based upon that, so say "boots = 4" it takes that, and applies the proper texture for the boots, you do it on start up of the character, and something like "if(updateclothes)" and at the end of that if put "updateclothes = false;" so it updates the look, and then stop updating it, so whenever they put new gear on just put "updateclothes = true;" and upon switching of scenes, it should refer to the start, and update.

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

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

Material doesn't have a color property '_Color' 4 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Changing two different objects renderer colour 1 Answer

Changing a materials color in C# 2 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