r/UnityHelp • u/Anon_cat86 • Apr 02 '26
PROGRAMMING How do i make a function wait until after a variable number of other objects have gone through their collision functions?
So here's the problem i'm having:
I'm trying to implement a fishing mechanic to my (2d) game. The game has oceans, with islands, some of which have lakes. When you fish, the player spawns in a fishing rod in front of them, and I need to determine whether the fishing rod is on land or in water.
the problem is, i need the fishing rod to work if in the ocean, but not work if on an island *and* in the ocean, unless the rod is in a lake and on an island and in the ocean.
The way I'm trying to do this is by having each lake, island, and ocean, have a trigger hitbox that activates and sets a variable on the rod itself if it gets hit. But, since the ocean, island, and lake each have a different script that runs at a different speed, how do i make the fishing function not activate until all 3 of the variables have been set, if they're going to be set, which they might or might not be since they only activate on collision and if the player isn't fishing on an islsnd, those collisions dont occur?
TLDR, how do i make a unity script wait until multiple other scripts *would have* resolved *if* they were running, which they may or may not be?
1
u/[deleted] Apr 02 '26
You may want use async/await functions. Theses functions resolves as promises and you could chain them like this :
await MyConditionA();await MyConditionB();await MyConditionC();// Code reaching here means all 3 conditions are a success1
u/PayalGames Apr 03 '26
You don’t need to wait for multiple scripts or worry about timing in Unity.
Instead of trying to sync ocean / island / lake triggers, just collect what is overlapping and decide once.
When fishing starts, reset everything
Each trigger just sets its value when the rod enters it.
Then after a very small delay (0.1 sec), evaluate once:
Rules:
public class FishingRod : MonoBehaviour { public bool isInOcean; public bool isOnIsland; public bool isInLake;
}
———-
Trigger
void OnTriggerEnter2D(Collider2D other) { FishingRod rod = other.GetComponent<FishingRod>();
}
Code Flow 1. Player presses Fish button 2. Rod spawns 3. StartFishingCheck() called 4. Wait 0.1 sec 5. Evaluate area 6. Start / Stop fishing