1

I am trying to save data for my app I am using this code:

BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/inventorySave.gd"); 
bf.Serialize(file, toSave);
file.Close();

I also tried this code:

StreamWriter sw = new StreamWriter(Application.persistentDataPath + "/inventorySave.txt");
sw.WriteLine(toSave);
print("wrote line" + toSave);
sw.Flush();
sw.Close();    

But none of them seem to work (my unity crashes and I don’t get any errors in console). Saving is called inside a for loop, and I am trying to save string with less then 8 characters the code can be found here.

Umair M
  • 10,298
  • 6
  • 42
  • 74
Patrik
  • 13
  • 5

1 Answers1

1

This is what I have found somewhere in your code:

for (int i = 0; i <= numbers.Length ; i++)
            {
                print("inside while class");
                switch (int.Parse(numbers[i--].ToString()))
                {
                    case 0:
                        finalString[i--] = "a".ToString(); break;
                    case 1:
                        finalString[i--] = "b".ToString(); break;
                    case 2:
                        finalString[i--] = "c".ToString(); break;
                    case 3:
                        finalString[0] = "d".ToString(); break;
                    case 4:
                        finalString[i--] = "e".ToString(); break;
                    case 5:
                        finalString[i--] = "f".ToString(); break;
                    case 6:
                        finalString[i--] = "g".ToString(); break;
                    case 7:
                        finalString[i--] = "h".ToString(); break;
                    case 8:
                        finalString[i--] = "i".ToString(); break;
                    case 9:
                        finalString[i--] = "j".ToString(); break;
                }

                i++;

In every iteration, this is what happens to variable i:

  1. int i = 0; // in start of for loop i is "0".

  2. switch (int.Parse(numbers[i--].ToString())) // i = -1 here

  3. finalString[i--] = "a".ToString(); break; // in any of case statement, i = -2

  4. i++; // i = -1 here
  5. i++; // i = 0 here

Hence : value of i will always be 0 and will never be greater than numbers.Length so loop will not terminate.

Hence : infinite loop -> Unity Crashes

Umair M
  • 10,298
  • 6
  • 42
  • 74
  • Last thing because that will tell you why it crashed. And if would be more helpful if you post **actual** code instead of just a snippet. – Umair M Aug 17 '16 at 13:34
  • last messages in log are tagged with "(this message is harmless)" and evrything else i rly dont understand – Patrik Aug 17 '16 at 13:40
  • **yes it is inside try catch, and it is looped 32 times, can loop make it crash?** regarding this, I would like to see full code snippet including loops and everything else in the method – Umair M Aug 17 '16 at 13:52
  • w8 do i need to give you premmission or smth?, I am first time using stack overflow... – Patrik Aug 17 '16 at 14:16
  • Don't worry I have permisions. Please post relevant code in answer next time instead of providing zip file. – Umair M Aug 17 '16 at 14:18
  • ahh ok sry; like I said, i am using this for first time, tnx for your advice :D! – Patrik Aug 17 '16 at 14:19
  • I'm pleased to be helpful. Please mark the answer as correct. – Umair M Aug 17 '16 at 14:45
  • 1
    @Patrik You can't upvote since you don't have enough reputation but you should accept this answer since it solved your problem. – Programmer Aug 17 '16 at 15:00