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 N.A.K_FYP · Mar 15, 2014 at 04:05 AM · c#gameobjectcolor

How can i change the gameobject color based on RGBA

I can get a string value RGBA(1.000, 1.000, 1.000, 1.000) from one gameObject

I know that it can change the gameObject color in runtime shown as below, but how can i make RGBA(1.000, 1.000, 1.000, 1.000) value to this gameObject?

 GameObject.Find("Cube").transform.renderer.materials[0].color=Color.red
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
2
Best Answer

Answer by robertbu · Mar 15, 2014 at 04:07 AM

Unity has the Color class. In C# you would do:

  GameObject.Find("Cube").transform.renderer.materials[0].color = new Color(1.0,1.0,1.0,1.0);

There is a constructor that takes three parameters so if you are not dealing with the alpha channel you can do:

 Color c = Color(0.5, 0.2, 0.6);
Comment
Add comment · Show 5 · 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 N.A.K_FYP · Mar 15, 2014 at 04:18 AM 0
Share

Thank you @robertbu

In program$$anonymous$$g, how can i make the the 1.000 in different varable that the code become:

 String color = RGBA(1.000, 1.000, 1.000, 1.000) 
 _red = 
 _green =
 _blue =
 _a =
 
 GameObject.Find("Cube").transform.renderer.materials[0].color = new Color(_red,_green,_blue,_a);
avatar image robertbu · Mar 15, 2014 at 04:41 AM 0
Share

Ahhh this is a string split and conversion problem. I did not understand that from your initial question. I tend not to answer these kinds of questions. Since I don't do much of them, my answers are brute force. There are many on this list that have very elegant solutions to these problem. So here is a brute force solution, but if you wait around for more answers, it is likely someone will give you a more elegant solution:

 function Start() {
     var colorString : String = "RGBA(1.000, 1.000, 1.000, 1.000)";
 
     var start = colorString.IndexOf("(");
     var length   = colorString.IndexOf(")") - start - 2;
     var s = colorString.Substring(start+1, length);
      
     var nums : String[] = s.Split(","[0]);
 
     var _red : float;
     var _green : float;
     var _blue : float;
     var _a : float;
 
     float.TryParse(nums[0], _red);
     float.TryParse(nums[1], _green);
     float.TryParse(nums[2], _blue);
     float.TryParse(nums[3], _a);
     
     Debug.Log(_red+","+_green+","+_blue+","+_a);
 }

Note I'm still confused by your question. How do you get a string color value from a game object. Is RGBA() a class I'm unaware of or something else? If this is a Unity color, you should be able to access a Color class value on that game object.

avatar image N.A.K_FYP · Mar 15, 2014 at 05:28 AM 0
Share

Thank you, i will try the solution ASAP,. however, is it similar to C#?

btw, the RGBA(1.000, 1.000, 1.000, 1.000) String value is get by

 itm.color = body.transform.renderer.materials[0].color.ToString();

i store RGBA(1.000, 1.000, 1.000, 1.000) in the X$$anonymous$$L string value, also , i need to load and read this value to set it into new gameobject. So, i need to change RGBA(1.000, 1.000, 1.000, 1.000)

avatar image robertbu · Mar 15, 2014 at 05:46 AM 0
Share

Ahhhh, now everything makes sense. Here is the C# translation of the above code:

 void Start() {
         string colorString = "RGBA(1.000, 1.000, 1.000, 1.000)";
         
         int start = colorString.IndexOf("(");
         int length   = colorString.IndexOf(")") - start - 2;
         string s = colorString.Substring(start+1, length);
         
         string[] nums  = s.Split(","[0]);
         
         float _red, _green, _blue, _a;
         
         float.TryParse(nums[0], out _red);
         float.TryParse(nums[1], out _green);
         float.TryParse(nums[2], out _blue);
         float.TryParse(nums[3], out _a);
         
         Debug.Log(_red+","+_green+","+_blue+","+_a);
 }
avatar image N.A.K_FYP · Mar 15, 2014 at 06:03 AM 0
Share

Thank you :> i will try it ASAP, because i have some problem for get the value form X$$anonymous$$L, i will try this after i can get the X$$anonymous$$L value.

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

21 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

Related Questions

Unable to change the color of a button on a disabled button. 0 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

Change sprite color on GameObjects from a list. 1 Answer

Distribute terrain in zones 3 Answers

GameObject keeps acting like gameObject. 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