Perfect IF does not match?

private Transform LocatePoint()
{
    if (File.Exists("customdata.save"))
    {
        Debug.Log("File found!");
        string[] array2 = File.ReadAllText("customdata.save").Split(new char[]
        {
            '\n'
        });
        string sceneName = array2[2];
        float x = Convert.ToSingle(array2[0]);
        float y = Convert.ToSingle(array2[1]);
        Vector3 position = new Vector3(x, y, 0f);
        Debug.Log(string.Concat(new string[]
        {
            "Scene: ",
            sceneName,
            " x: ",
            x.ToString(),
            " y: ",
            y.ToString(),
            " position: ",
            position.ToString(),
            " PdataRespawnScene: ",
            this.playerData.respawnScene.ToString(),
            " GM_scene: ",
            this.gm.sceneName.ToString()
        }));
        if (sceneName.ToString() == this.gm.sceneName.ToString())
        {
            Debug.Log("Scene name confirmed!!");
            RespawnMarker respawnMarker = new GameObject().AddComponent<RespawnMarker>();
            respawnMarker.name = "Death Respawn Marker";
            respawnMarker.tag = "RespawnPoint";
            respawnMarker.transform.parent = GameObject.Find("_Markers").gameObject.transform;
            respawnMarker.transform.position = position;
            Debug.Log("All done!");
        }
    }
    //does its own thing
}

Console output:

I’m a bit confused. Why if (Scene1 == Scene1) returns false?

Try using the .Equals method instead, although someone may have a better suggestion for checking if your scene matches.

While it would be strange if this were the cause of the issue, I will note that you’re outputting “sceneName” in your debug but using “sceneName.ToString()” in your if.

Also it’s possible there are hidden characters or whitespace you can’t see in your debug output. Output the .Length of each string and see if they’re the same length as well. Actually, looking at your debug output, there does appear to be a newline at the end of sceneName, which would cause your problem.

2 Likes

StarMana
Hmm, but how could be the case, though? I explicitly read lines 0-3. File is written by File.WriteAllLines

File.WriteAllLines("customdata.save", new List<string>
            {
                position.x.ToString(),
                position.y.ToString(),
                this.gm.sceneName.ToString()
            }.ToArray());

it looks like:

14.064
84.40561
Scene1

There is indeed an empty line 4 (index 3), but I’m curious as to why it is being read along with line[3]?

EDIT:
Nvm, guess I will have to use ugly last line removal:

My suspicion is that this may be an issue between standard newline (\n) and carriage returns (\r) or some other line ending confusion. You may try modeling your Split statement based on this SO discussion.

1 Like