Script doesn't properly reference.

Hey everyone,

I’m trying to reference two objects in an array, then get their respective individual values to decide whether or not the if statement below should run on them.

The first debug.log properly prints the names (waterfallScript.name) of both objects, but the second debug.log after the if statement only prints the name of the first.

The rest of the code (the transform and coroutine) only work on the first one as well.

It’s worth noting that altering the first object does affect the second.

It’s like the second object gets lost somehow once I go into the if statement.

public void Waterfalling()
{

    foreach (GameObject waterfalls in _player.waterFalls)
    {
        var waterfallScript = waterfalls.GetComponent<Waterfall>();
        Debug.Log(waterfallScript.name);
        if (!waterfallScript.frozen && waterfallGo && !_player.isTurning)
        {
            Debug.Log(waterfallScript.name);
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y + fallSpeed);
            StartCoroutine(WaterfallTimer());
        }
    }

}

Thanks for reading, hopefully someone knows what’s going on.

Your logical condition isn’t true when passing in the second object (i.e one of the conditions is false). I’m not sure how the “waterfallGo” and “_player” variables are behaving outside of this block of code, but I would guess the “frozen” variable on the second object is true (making it false with the “!” operator). The easiest way to determine this is to set a breakpoint on line 8 (the if statement), start the project in Visual Studio (or whatever IDE you’re using), and run the program in Unity. Once the program hits that line of code, it will pause and you can hover over the “waterfallScript” to see if the “frozen” variable is true or false.