- Home /
How do I tell another script to skip its routine for one frame?
I'm simulating gravity in space using Newton's law of gravitation. How I'm going about it is by making a script Gravity
that has a static list of Gravity
classes that consists of all the objects containing the script Gravity
in the scene. Then I go through the list and run the function Gravitate()
on each of them and feed into it all of the objects so that it can calculate a gravitational force for itself.
My question is: how do I go about this without running the same operation for each of the object that is effecting each other. In other words, how do I calculate the force for my object, tell the other object its force as well and tell it not to run its own function as I've already calculated its force?
I thought I could somehow use coroutines for this but I'm not experienced enough. Telling the other script to skip this frame or something like that.
Invoke https://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html using GetComponent. Yes, Coroutine.
Answer by DaoGear · Jan 06, 2018 at 05:41 AM
you maybe need a static bool flag in Gravity.cs
// put this in Gravity.cs
public static bool isCalculate;
// and somewhere
void Foo() {
for (int i = 0; i < gravityList.Length; i++) {
if (Gravity.isCalculate) {
if (i == gravityList.Length - 1) {
Gravity.isCalculate = false;
}
} else {
// Calculate and set Gravity.isCalculate = true
}
}
}
Your answer
Follow this Question
Related Questions
check bool in WaitForSeconds 2 Answers
Delay Jump 1 Answer
How to create a delay in a function being called in update? 2 Answers
Time Delay Animation 1 Answer
Make a function that can return string or IEnumerator 2 Answers