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
1
Question by youngapprentice · Aug 17, 2012 at 04:17 PM · javascriptstringissue

String.Split() Function Issues

Hi, all! I am saving data currently with PlayerPrefs for my web game. There is a good amount of data, and 3 save slots. My short solution to this is taking all variables that need to be saved, compiling them into a string, and inserting a Pipe character (" | ") Between the variables. Then I go to split the string when I load it using this:

 function LoadSlot(){
 //Not actual loaded variable. Made-up one for testing purposes
     var LoadStr = ("KHizzle|color.white|4252|010110|2434|123456789012345678901234567890123456789012");
     //var LoadStr = PlayerPrefs.GetString("Slot"+CurrentSlot);
        // The above line is the real code to load it, but since I am debugging it, I supply the long string myself.
     LoadStr.Split("|"[0]);
     Name = LoadStr[0].ToString();
     Debug.Log("Loaded Name as " + Name);
 }

My problem here is that when I run this code, I get an

IndexOutOfRangeException: Array index is out of range.

Exception. And the Debug, when I insert a '1' instead of a '0' gives me this:

Loaded Name as H

Which should not be. It should be separating AT the pipe sign and not after every character, right? LoadStr[1] should be: color.white

How do I fix this issue? Thanks!- YA

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

Answer by SolidSnake · Aug 17, 2012 at 04:19 PM

LoadStr is a string variable you need an array to store the values returned from Split function.

 String[] array = LoadStr.Split("|");

==Parsing example==

 // in JS
 var myLongValue: long = long.Parse(myValueInString);
 var myColor: Color = new Color(float.Parse(myRedValueInString),float.Parse(myGreenValueInString),float.Parse(myBlueValueInString));
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 youngapprentice · Aug 17, 2012 at 04:45 PM 0
Share

Thank you so much! Lastly- I know I can get something ToString();, but what if I need to convert it to a Color, or an Int?

avatar image SolidSnake · Aug 20, 2012 at 07:58 AM 1
Share

.net 2.0 API allows you to do this.. have a look at the int.Parse() and int.TryParse() (or in your case you might need to use long ins$$anonymous$$d of int(Int64))

for the color you will need to store the RGB values and then convert them to color using the Color Class.. I will update my answer with example

avatar image youngapprentice · Aug 20, 2012 at 06:53 PM 0
Share

Ah I see. thank you so much!

avatar image youngapprentice · Aug 20, 2012 at 06:54 PM 0
Share

Now I just need to create a for loop to split up this: "1.0000.0000.0001.000" into 4 different variables. I can do that with substring right? Doesn't the second value deter$$anonymous$$e the length of the returned substring?

avatar image SolidSnake · Aug 21, 2012 at 07:54 AM 0
Share

are these the values of the rgba? did you store them in this format (i.e. separated by dot) or it was given to you this way?

if you are storing them yourself, it will be easier if you store them separated by a different character than a dot e.g. an underscore '_' so you can use split ins$$anonymous$$d. Substring can be used if you always have the same length of your variables and yes when you use substring then the second value deter$$anonymous$$e the length of the substring

Show more comments

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

9 People are following this question.

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

Related Questions

Multiple Animations for the Same GameObject 2 Answers

character lagging in air when jumped 0 Answers

Why Am I getting MissingMethodException message? 3 Answers

Talking Code 2 Answers

Not getting a Debug.Log 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