- Home /
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.
The if statement is only executing for the first. Either the if condition always fails for the second, or when the if statement executes, it changes something which makes it fail. If it always fails for the second, maybe waterfallScript.frozen is true. If not, maybe it's something in the WaterFallTimer coroutine.
Answer by zblase · Apr 07, 2020 at 06:26 AM
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.
I have both objects present in the scene.
One object's frozen is true, the other is set to false. The if condition executes on both even though it shouldn't on the second.
When I set the first one to false the second one stops as well. Completely ignores it's own script.
The waterfallgo value belongs to a coroutine in the script that intermittently stops the code because I wanted brief delays.
IEnumerator WaterfallTimer()
{
waterfallGo = false;
yield return new WaitForSeconds(0.12f);
waterfallGo = true;
}
The _player.isTurning variable just makes sure the code doesn't execute when the player is turning, works fine.
The issue really is the second GameObject not checking it's own frozen value.
Acts like I never even bothered to get it.
The foreach statement will continue to loop even if you're waiting x seconds in the coroutine. Ergo, when the first loop calls the WaterfallTimer coroutine, it's setting "waterfallGo" to false, so when your second loop hits that if statement, it won't execute the code inside said if statement because "waterfallGo" is false. If you remove the "waterfallGo = false", the second object will execute the code inside the if statement. I'm sure that's probably not the exactly functionality you're looking for but that's not something I completely understand at the moment. If you want the second object to execute that code 0.12 seconds after the first, I'd recommend using a while loop and waiting for "waterfallGo" to be true.
I just completely omitted the WaterfallTimer script and thus the waterfallgo value for testing purposes.
I'm still having the same issue.
The functionality I'm looking for is for the condition to execute if the object's frozen is set to false. As it stands it doesn't work on any of my objects, unless I add a breakpoint and manually click the serialized $$anonymous$$en value.
And even then it only works on one of my GameObjects, the second one bases it behavior on the first.
Perhaps some additional info will help: It concerns two waterfalls in an array. I'm telling them to only cascade if they aren't frozen. In my scenes I have a frozen waterfall and one unfrozen one.
The problems:
1 Once I run my code both my frozen and unfrozen waterfall start cascading.
2 Freezing them through code doesn't stop the code, only manually clicking the Serialized field ''$$anonymous$$en'' does.
3 Even the, only the first one works. Second object does whatever the first does.
Your answer
Follow this Question
Related Questions
Assign RectTransform to Array based on another Array 0 Answers
How to get all components that contain Color field 2 Answers
How to use a foreach loop to disable scripts on objects within a GameObject array? 1 Answer
Vector3.Lerp curving along three destinations 1 Answer
Array of objects - script only works on the first object in the array 2 Answers