I have a player, who at runtime could have one of two scripts. Both scripts have a function called “Execute”, Which runs the main body of the script. both scripts have a AB_ before the names. I need, at runtime, to call one of the scripts. How would i go about executing “Execute” from one of the scripts?
Use an interface, that way you can do GetComponent on the interface and call execute from there.
I guess you Code with C#. So you need to set your Execute functions to Public.
using UnityEngine;
using System.Collections;
public class AB_YourFirstScript : MonoBehaviour
{
public void Execute() {
// Do Something...
}
}
Now in the Script you want to Call the Execute functions you can put this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExecuteScript : MonoBehaviour {
public AB_YourFirstScript AB_one;
public AB_YourSecondScript AB_two;
void Start(){
//if you dont want to asign them Manually
AB_one = GameObject.FindGameObjectWithTag("Player").GetComponent<AB_YourFirsSctipt>();
AB_one = GameObject.FindGameObjectWithTag("Player").GetComponent<AB_YourSecondSctipt>();
}
void Update () {
AB_one.Execute ();
// or
AB_two.Execute();
}
}
if you need to Call Execute() more than once, but the Trigger Method is just Once like OnKeyDown you ned to make a coroutine.
hope this helps.