- Home /
How can I call a new class function with InvokeRepeating ?
public class CheckInRange{
public IEnumerator loopCheckInRange(){
timer += 1;
Debug.Log (timer);
yield return new WaitForSeconds (0.2f);
}
}
In the other class:
public class test : MonoBehaviour {
public CheckInRange checkInrange = new CheckInRange ();
// Use this for initialization
void Start () {
InvokeRepeating("checkInrange.loopCheckInRange",0f,0.2f);
}
}
Answer by Bunny83 · May 05, 2020 at 12:03 AM
That's not possible InvokeRepeating does only work for methods defined inside the MonoBehaviour where you call the InvokeRepeating on. It can not call methods on some other objects. Even if it would work your code wouldn't make much sense. You did not create a method but a coroutine. However a yield at the end of the coroutine is completely pointless as the coroutine will wait for the time specified and then it does nothing and just finishes.
It's impossible to determine your actual goal from your code snippet so we can't really suggest an alternative without knowing what you actually want to achieve.
Thank you for your answer,so have some simple method for repeating function in the new class?
I want to make a independent function for checking distance between 2 object for 0.2f/1times.
Your answer
Follow this Question
Related Questions
InvokeRepeating inside of a class 2 Answers
Overriding a function. 1 Answer
Using reference without object (no new)? 2 Answers
Help integrating a category system 0 Answers
"Null Reference Error" when using a custom class as an array 1 Answer