Search Unity

Resolved value not incramenting?

Discussion in 'Scripting' started by larswik, Dec 17, 2022.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I must have gotten up on the wrong side of the bed this morning. This is so easy and yet my results are not what I expected. I am parsing a string from a file. I split the string and the array that it creates has the level name and text description of the mission coming up on the next index. I search the array for the level name and return the index int, I then increment that value by one to get the description, located in the next index of the array. But it will not increment? In the photo you can see the value is still 0, when it should be 1. It seems so simple that I must be missing some obvious answer? Any help is appreciated. I have been looking at this for a couple hours.
    Code (CSharp):
    1.     private void UpdateLevelMissionBrief()
    2.     {
    3.         int currentLevel = PlayerPrefs.GetInt("Level");
    4.  
    5.         string levelNameToFind = "Level" + currentLevel.ToString();
    6.         string missionBriefPath = "Assets/Resources/docs/mission_brief/mission_briefs.txt";
    7.         string wordfile = File.ReadAllText(missionBriefPath);
    8.         string cleaned = wordfile.Replace("\n", "").Replace("\r", "");
    9.         string[] wordFileArray = cleaned.Split("*");
    10.  
    11.         int value = Array.FindIndex(wordFileArray, indexNum => indexNum == levelNameToFind);
    12.         value += 1;
    13.         missionBrief.text = wordFileArray[value];
    14.         print(" is the index of: " + value);
    15.  
    16.  
    17.     }
     

    Attached Files:

  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Why don't you check it before you add 1? FindIndex will return -1 if it isn't found.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    OMG... That was it. Thank you! I was so focused on incrementing that value by 1, and it was but the array was returning a -1, which it was than incrementing it to 0. Once you pointed that out I found the error above causing it to give me the correct answer. Thanks!
     
    RadRedPanda likes this.