Hi, im not sure if it does exist, but i dont know how to do somthing like that:
public void myFunction(Component **script**)
{
gameObject.GetComponent<**script**>().function;
}
If anybody know how to do, pls help me if u can.
Thanks
Hi, im not sure if it does exist, but i dont know how to do somthing like that:
public void myFunction(Component **script**)
{
gameObject.GetComponent<**script**>().function;
}
If anybody know how to do, pls help me if u can.
Thanks
If I understand the intention correctly, which is to call the function on the given type of script, then there are a few ways to do this. Not sure if this is suitable for your exact problem, but an OOP organized way with generics would be something like this:
using UnityEngine;
public class TestClass : MonoBehaviour
{
private void Start()
{
gameObject.AddComponent<ExampleFunctionClass1>();
gameObject.AddComponent<ExampleFunctionClass2>();
DoFunction<ExampleFunctionClass1>();
DoFunction<ExampleFunctionClass2>();
}
private void DoFunction<T>() where T : MonoBehaviourWithFunction
{
gameObject.GetComponent<T>().Function();
}
}
public abstract class MonoBehaviourWithFunction : MonoBehaviour
{
public abstract void Function();
}
public class ExampleFunctionClass1 : MonoBehaviourWithFunction
{
public override void Function()
{
Debug.Log("Function from 1!");
}
}
public class ExampleFunctionClass2 : MonoBehaviourWithFunction
{
public override void Function()
{
Debug.Log("Function from 2!");
}
}
This works because C# knows any type derived from MonoBehaviourWithFunction will have the Function() and the DoFunction<T>() knows that you will tell it which type to use.