- Home /
Duplicate Question
Invoke on Struct or some else time wait
using UnityEngine;
using System.Collections;
public struct SomeStruct {
public void example (){
Invoke("example1",2);
}
void example1 (){
Debug.Log("asdgsfdgjhsdfgj");
}
}
I get an error: Assets/Email.cs(6,17): error CS0103: The name `Invoke' does not exist in the current context
but if I do same thing in class monobehaviour I have no problems whatsoever
is there any possibility to do this in structure?
as I've found out that invoke works in update and in start so I do not understand why can't it work in structure
but if I'd do it this way:
using UnityEngine;
using System.Collections;
public struct SomeStruct {
public IEnumerator Example() {
Debug.Log(""+Time.time);
yield return new WaitForSeconds(1);
Debug.Log(""+Time.time);
}
public void Example1(){
Debug.Log(""+Time.time);
}
}
and if I call example I get nothing at all
and if I call example 1 I get normal debug as I should
and OFC I'm calling it from start
Answer by Ashkan_gc · May 19, 2013 at 12:15 PM
Well it's because Invoke is a member of MonoBehaviour class and only classes derived from it can call it, So you can not call Invoke in a struct. read about OO concepts and inheritance in a book or on the web to know more about it.
Simulating the functionality is possible using reflection and members of the system.reflection namespace but it's a little harder to do. There was a unite talk on reflection and the book programming C# 3 by j.liberty describes it as well. Take a look if you are interested. Basically you get the type of an object using GetType() method or typeof() operator and the using reflection get MethodInfo object of the method that you want by passing it's name and then you can call/invoke that methodInfo.
ok thanks for your effort :)
while playing some more I've figured out I can't do it with invoke as you said so I went looking in to yield some more and I figured out why didn't it work
well because I was only calling the function Example While I should Call it
StartCoroutine(Em.Example());
and when I did that both ways worked Start and update :)
so thanks for your effort even if it wasn't totally right answer but it did pushed me in to the right way :)