- Home /
Different game object position in editor/windows build than in android build
Hi, I got an issue positioning game objects at runtime when my project was built for android. The script works well in editor and in a windows build, but not in mobile.
My project : It is a 2D project. About 300 gameobjects are positioned at the right outside of the screen and are not moving. They are precisely positioned with various steps on x axis between each of them. At start, 20 of them move with a constant speed from right to left and appear on the screen. Once the most right game object (lets call it mrgo) reach the right screen limit, the 20 next game objects (not present in the screen) are teleported at a precise position after the mrgo gameobject to get back the initial position before start. GameObjects are teleported like this 20 by 20.
My problem : When built for windows or in the editor, gaps between each gameobjects are preserved. But when I start the game in an android mobile, few gaps are not respected. Those gaps seems to concern value with points. EG : On mobile, gaps like 1 or 2 between gameobjects are respected. But gaps like 1.5f, 0.5f or 0.25f are not. It seems that the bigger the decimals are, the bigger are the gaps. Eg : 0.5f let a certain space. 0.25f possibly double or triple it.
Again this behavior only concern android build and not windows or editor.
Do you have any suggestion to try to find the problem ? Have you ever heard about problems at positioning gameobjects in android platform ?
All thanks for your help !
Answer by ludwu · Nov 13, 2019 at 01:38 PM
I finally found the solution. For a reason I don't understand, parsing a string to get a float value couldn't be done under an android build. My old code :
Vector2 SetPosition(GameObject[] lastGo, GameObject currGo, int index)
{
Vector2 pos;
string strCopy;
float duration;
strCopy = lastGo[index].name.Remove(0, 5);
duration = float.Parse(strCopy);
pos = new Vector2(lastGo[index].transform.position.x + duration * 4f, currGo.transform.position.y);
return pos;
}
The code was ok with integral result values, but created errors with floating result values.
In new code :
Vector2 SetGoPosition(GameObject[] lastGo, GameObject currGo, int index)
{
Vector2 pos;
pos = new Vector2(lastGo[index].transform.position.x + durationArray[index] * 4f, currGo.transform.position.y);
return pos;
}
Now I use an array with durations that are already set up. Problem solved, but still incomprehensible. If you understand why, please tell me :)
Answer by ludwu · Nov 12, 2019 at 10:05 PM
I tried to upgrade Unity, but there's still the problem. Anyone have any idea?
Answer by Bonfire-Boy · Nov 13, 2019 at 05:18 PM
I suspect it's to do with different cultures. Some use commas and some use periods to separate the mantissa from the decimal part, and float.Parse uses your current culture by default.
You could verify this is the issue by logging the value of System.Globalization.CultureInfo.NumberFormat.NumberDecimalSeparator. If I'm right then it will differ between working and non-working devices.
If this is the problem, then you can force parse to use the right separator with something like this...
System.Globalization.CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = "."; // or "," depending what you need
float number = float.Parse("0.54", culture);
Hey ! That's very interesting ! Didn't think this way. Indeed, that was the trick :)