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
1
Question by UnityGuy1243 · Aug 10, 2011 at 03:44 AM · colorstringintparsingmissingmethodexception

Parsing a String to a Color

How come this works:

 var idk1 : String = "123";
 var idk2 : int = int.Parse(idk1);
 print(idk2);

But this doesn't:

 var idk1 : String = "(1.0, 1.0, 1.0, 1.0)";
 var idk2 : Color = Color.Parse(idk1);
 print(idk2);

I get this error: MissingMethodException: Method not found: 'UnityEngine.Color.Parse'.

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
6
Best Answer

Answer by Peter G · Aug 10, 2011 at 04:02 AM

Because there isn't a Color.Parse() method. You can't just make up methods. You need to check the API to see if they exist. int.Parse() is a real method in the .Net library. It wouldn't be very hard to write a parser for a string though:

 public static class ColorExtensions {
     public static function ParseColor (col : String) : Color {
         //Takes strings formatted with numbers and no spaces before or after the commas:
         // "1.0,1.0,.35,1.0"
         var strings = col.Split(","[0] );
 
         var output : Color;
         for (var i = 0; i < 4; i++) {
              output[i] = System.Single.Parse(strings[i]);
         }
         return output;
     }
 }

That should work. It will give you a warning saying that declaring a static function in a static class is redundant, but I prefer to leave it in for clarities sake. It isn't a harmful warning.

Example:

 var col = ColorExtensions.ParseColor( "0.0,1.0,.75,.2" );
 Debug.Log(col);
Comment
Add comment · Show 6 · 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 UnityGuy1243 · Aug 10, 2011 at 04:18 AM 0
Share

Quick question, can you or someone else give me a link to the api your using for these functions? Because I can't find it on here:

http://msdn.microsoft.com/en-us/library/ms753307.aspx

avatar image Eric5h5 · Aug 10, 2011 at 05:20 AM 0
Share

@UnityGuy1243: Unity doesn't use WPF. http://msdn.microsoft.com/en-us/library/ms229335(v=VS.90).aspx

avatar image meat5000 ♦ · Feb 10, 2015 at 02:20 AM 0
Share

Your Parser was useful. +1 Peter.

avatar image Bunny83 · Jul 02, 2016 at 11:17 AM 0
Share

I suggest to use this ins$$anonymous$$d:

 var output = Color.white;
 var count = $$anonymous$$athf.$$anonymous$$in(strings.Length, 4);
 for (var i = 0; i < count; i++) {

This allows you to omit the alpha value and avoids errors if you pass to many values.

avatar image halleford · Aug 23, 2016 at 04:52 PM 0
Share

Newbie here...

Where do you place this script? I tried simply creating a new C# script in Unity, and cutting/pasting your code... I got a ton of red squiggles.

avatar image Bunny83 halleford · Aug 23, 2016 at 06:29 PM 0
Share

Well, it doesn't really matter where you put it, but this is not C# code. This is UnityScript (Unity's javascript dialect). So you would need to create a "Javascript" script. However if you use C# (and i would recommend that) you want to translate it into C# ^^.

 // C#
 public static class ColorExtensions
 {
     public static Color ParseColor(string aCol)
     {
         var strings = aCol.Split(',');
         Color output = Color.black;
         for (var i = 0; i < strings.Length; i++)
         {
             output[i] = float.Parse(strings[i]);
         }
         return output;
     }
 }

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert a string to an int? 1 Answer

int to char conversion in JS ? 2 Answers

Type 'int' does not support slicing 1 Answer

Part of string not showing 1 Answer

How to reference a variable in another script based on string in c# 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