Question by
DaiMangouDev · Jan 08, 2017 at 01:35 AM ·
c#methodcallback
How to call a non-static method from another class without using an instance
Hi , i am trying to figure out how to
call this function in my Class1 class from my Class2 Class
Public class Class1 : MonoBehaviour
{
public void SayHello( string name)
{
Debug.Log("Hello " + name);
}
}
my other class
public class Class2 : MonoBehaviour
{
void OnEnable()
{
// Call the SayHello method
}
void OnDisable()
{
// Call the SayHello method
}
}
Comment
Best Answer
Answer by Smaika · Jan 08, 2017 at 04:53 AM
The way I like to do it is have a static reference of class1 so this is how to do it:
Public class Class1
{
public static Class1 C1;
void Awake (){
C1 = this;
}
public void SayHello( string name)
{
Debug.Log("Hello " + name);
}
}
public class Class2
{
void OnEnable()
{
Class1.C1.SayHello("It's working ;)");
}
void OnDisable()
{
Class1.C1.SayHello("It's working ;)");
}
}
That way you would be able to call SayHello on Class1 easly from any other class