Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question method returns wrong values on specific system (win 11 issue?).

Discussion in 'Windows' started by MasterLorian, Oct 17, 2022.

  1. MasterLorian

    MasterLorian

    Joined:
    Sep 14, 2022
    Posts:
    2
    I am using this method to extract min/max values from a string and generate a random value. the string format is like this [value1,value2]. It works perfectly on the two win 10 systems that i tested the game on, but in a third system with win 11 it only returns non decimal values. example : This string is inserted as an argument "AlterWeaponPower[0.05,0.15]". in the first two systems it returns a number between 0.05 and 0.015. in the third system it returns a number between 5 - 15.

    what could be the cause of this madness?

    Code (CSharp):
    1.      float ExtractValue(string effect)
    2.      {
    3.          float finalValue;
    4.          int indexA;
    5.          int indexB;
    6.          float[] values = new float[2];
    7.          indexA = effect.IndexOf('[') + 1;
    8.          indexB = effect.IndexOf(',');
    9.          values[0] = float.Parse(effect.Substring(indexA, indexB - indexA));
    10.          indexA = effect.IndexOf(',') + 1;
    11.          indexB = effect.IndexOf(']');
    12.          values[1] = float.Parse(effect.Substring(indexA, indexB - indexA));
    13.          Log.current.SetText(effect+ "<br>"+"min : "+values[0] +"<br>" + "max : " + values[1]);
    14.          finalValue = RandomizeValue(values[0], values[1]);
    15.          return finalValue;
    16.      }
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,517
    Not all locales use period ('.') character as the decimal separator. Many parts of the world use the comma (',') as a decimal separator. So if the computer in question uses commas as a decimal separator, your code will fail. To force "float.Parse" use period as the decimal separator, pass in CultureInfo.InvariantCulture to it.

    In general, when parsing numbers in strings it's useful to always consider what locale you're parsing them from.
     
  3. MasterLorian

    MasterLorian

    Joined:
    Sep 14, 2022
    Posts:
    2
    Thanks for the answer. I ended up manually setting the locale at the game's startup.