Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 LesPaul0689 · Mar 23, 2016 at 01:03 PM · uimaterialcolorslider

Change material color with a three slider

Hi everyone,

I'm a beginner in programming and I'm trying to change the color of my material by using a UI with 3 sliders. I manage to change the color with the slider but it's only affect the script and not the material (I create a visualization of the color in the script, that I can see in the inspector of my mesh).

I'm very closed, I just need to match my color with the one of material and it will be good and I can't found how to do it.

Here the code:

 public Color altColor;
 public Renderer rend;
 public Material colors;
 void slider()
 {
     altColor.r = 0.5f;
     altColor.g = 0.5f;
     altColor.b = 1.0f;
 }
 void Start ()
 {
     slider();
     Renderer rend = GetComponent<Renderer>();
     rend.material.color = altColor;
     //Material rend = GetComponent<Material>();
     //rend.material.colorRed = altColor;
 }
 
 public void colorRed (float newRed)
 {
     altColor.r = newRed;
 }
 public void colorGreen(float newGreen)
 {
     altColor.g = newGreen;
 }
 public void colorBlue(float newBlue)
 {
     altColor.b = newBlue;
 }
 // Update is called once per frame
 void Update () {
 
 }

Comment
Add comment · Show 2
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 AlucardJay · Mar 23, 2016 at 04:08 PM 0
Share

This modifies the altColor variable, but then you need to set the material colour again after the change :

 public void colorRed (float newRed)
 {
     altColor.r = newRed;
     rend.material.color = altColor;
 }
avatar image LesPaul0689 · Apr 06, 2016 at 03:42 AM 0
Share

Thanks for the reply alucardj and Graphics_Dev.

I knew I was close and the "rend.material.color = altColor;" work perfectly !! Thanks you so much.

And sorry for the late answer ;)

1 Reply

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

Answer by Graphics_Dev · Mar 23, 2016 at 03:51 PM

Thanks @alucardj for the corrective comment...here is my updated answer. (I removed the slider function and call because I'm assuming that's there only for testing?)

 using UnityEngine;
 using System.Collections;
 
 public class ColorChanger : MonoBehaviour {
     public Color altColor;
     private Renderer rend;
 
     void Start ()
     {
         // Make sure you only get the rend component here
         // because when you get the color, you only store the
         // values of the color, not access to the color of 
         // the material.
         //
         // Also, 'rend' accesses the declared, private renderer
         // so this CAN'T be:
         //
         // Renderer rend = GetComponent<Renderer>();
         //
         // because that would store a new 'rend' that would not be 
         // accessible outside of this function.
         rend = GetComponent<Renderer>();
     }
 
     // set r, g, and b floats with slider
     public void colorRed (float newRed)
     {
         altColor.r = newRed;
         // Here we get the color of the 'gotten' Renderer
         rend.material.color = altColor;
     }
     public void colorGreen(float newGreen)
     {
         altColor.g = newGreen;
         rend.material.color = altColor;
     }
     public void colorBlue(float newBlue)
     {
         altColor.b = newBlue;
         rend.material.color = altColor;
     }
 }
 

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 AlucardJay · Mar 23, 2016 at 04:11 PM 1
Share

The OP is calling the public functions from a UI slider. While setting the material colour in update will fix the problem, a more optimal solution would be to set the material colour after modifying the variable rather than in update.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Changing two different objects renderer colour 1 Answer

How to lerp smoothly between two numbers? 1 Answer

UI Triangle Color picker 0 Answers

change color with playerprefs using buttons, between scenes.,How to use playerprefs to save colors from a button? 3 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