Is there a way to use the inspector to set a function in one script to call a function in another script on the same GameObject? Kind of like overriding from a child class but I am trying to avoid inheritance, so if I need the extra functionality for a method I will just call it from another component in the same object.
Right, turns out it wasn’t that hard to find and I just hadn’t understood what I was looking at. For anyone interested, it is quite a simple thing.
Base script to call from:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class TestBase : MonoBehaviour {
//This creates an interface in the inspector to assign functions
public UnityEvent OtherFunctions;
void Start () {
}
void Update () {
}
//This is the function that will be called and will trigger another function
void CallOtherFunctions()
{
OtherFunctions.Invoke();
}
}
And below is the extension script just for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestExtension : MonoBehaviour {
void Start () {
}
void Update () {
}
//This is the function that will be called from the other script
public void FunctionToBeCalled()
{
}
}
Now in the inspector, add both of these scripts to one object, this is what you will see. Click the + indicated to add a function:
Drag the test extension script below into the object field (Indicated below). You will then be able to select the function you wish to use (Again, as indicated).
Hope this helps everyone out.