• 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 PvTGreg · Feb 19, 2015 at 11:51 AM · subtracting

loading part of string from file

Hi in my project i have to save and load data from a file i have the saving down but when trying to use substrings to extract certain bits it dosent seam to work i get this error

ArgumentOutOfRangeException: startIndex + length > this.length Parameter name: length System.String.Substring (Int32 startIndex, Int32 length) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/String.cs:356) LoadScore.OnMouseOver () (at Assets/LoadScore.cs:22) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

saving the score

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Collections.Generic;
 
 public class saveScore : MonoBehaviour {
     public int Tank1Score;
     public int Tank2Score;
     public int Tank3Score;
     // Use this for initialization
     void Start () 
     {
         Tank1Score = PlayerPrefs.GetInt("Blues Score");
         Tank2Score = PlayerPrefs.GetInt("Greens Score");
         Tank3Score = PlayerPrefs.GetInt("Reds Score");
 
     }
     
     void OnMouseOver()
     {
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Debug.Log("Score Saved");
             //Get a binary formatter
             var b = new BinaryFormatter();
             //Create a file
             var f = File.Create(Application.dataPath + "/highscores.txt");
             //Save the scores
             b.Serialize(f, Tank1Score);
             b.Serialize(f, Tank2Score);
             b.Serialize(f, Tank3Score);
             f.Close();
             
             System.IO.File.WriteAllText("E:/score.txt", "Tank1Score:" +Tank1Score
                                         +"  "+"Tank2Score:"+Tank2Score
                                         +"  "+"Tank3Score:"+Tank3Score );
         }
     }
 }
 


 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Collections.Generic;
 
 public class LoadScore : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     void OnMouseOver()
     {
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Debug.Log("Score Load");
             string readText = File.ReadAllText("E:/score.txt");
             string tank1 = readText.Substring(11,11);
             string tank2 = readText.Substring(25,25);
             string tank3 = readText.Substring(39,39);
             Debug.Log(tank1);
             Debug.Log(tank2);
             Debug.Log(tank3);
         }
     }
 }
 



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 PvTGreg · Feb 20, 2015 at 11:42 AM 0
Share

this is what the save file looks like BlueTank:4 GreenTank:1 RedTank:3

so am i right in substringing 10,10 for the 4 and 23,23 for the 1 and 34,34 for the 3 as it dosent seem to work i get an ArgumentOutOfRangeException: startIndex + length > this.length and also it only comments " GreenTan"

avatar image PvTGreg · Feb 20, 2015 at 11:48 AM 0
Share

i got it string Blue = readText.Substring(9,2); i didnt quite know how to work it i pick character 9 and then 2 numbers after it

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tuncer · Feb 19, 2015 at 12:06 PM

Your problem is about exceeding length of string, be careful about starting index and length.

 Debug.Log(yourString.Substring(startingIndex, length));
 
 Debug.Log("abcdefghijklm".Substring(2, 4)); //output : cdef
Comment
Add comment · Show 3 · 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 PvTGreg · Feb 19, 2015 at 12:45 PM 0
Share

do spaces count as parts? as im counting them when trying to to figure out the number i want to extract

avatar image Tuncer · Feb 19, 2015 at 12:50 PM 0
Share

Yes, it does. Debug.Log("abc defghijklm".Substring(2, 4)); //output : "c de"

avatar image PvTGreg · Feb 19, 2015 at 02:15 PM 0
Share

ok so see when i substring the first part(tank1) does that lower the amount of the original string? so that i need to cut out a differant number?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Gameobject - gameobject 1 Answer

Timer not subtracting... 1 Answer

Sometime when I subract a value to hit zero I get a -1 1 Answer

Changing Variable OnMouseClick? Help! 1 Answer

Cooldown Timer Help! 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