I am wondering how to make a function and use it in another script without getting it from another gameobject.
You gotta be more specific if you want proper support
You can use ‘regular’ classes and make a new class variable in code.
You can use stuff like singleton pattern if that fits.
You can use completely static classes and methods if that fits.
If you don’t know the gameobject you can trigger an UnityEvent. You will need to declare this event in an object known to both scripts though. In a GameManager script for example.
Then it still goes via a reference to a gameobject (but unity events definitely are amazing to work with!)
Yes, you can use a static class though if you are hard against using gameobjects.
public static UnityEvent<int> myEvent;
Would that still be serializable?
You can use them without the inspector.
GlobalClass.myEvent = new();
GlobalClass.myEvent.AddListener(...);
GlobalClass.myEvent.Invoke(...);
This is not a static class it’s just a static field.
If you’re doing this there’s no reason to use UnityEvent. Just use normal C# delegates and events.
Thanks for the information, this is very useful.