agiro
agiro

Reputation: 2080

Create events that fires off a coroutine from another script

I'm making an event that does the failLevel stuff when it fires off. For that I have made a delegate

    public delegate Coroutine FailGame(IEnumerator function);
    public static event FailGame gameFailedEvent;

like so and I subscribed the appropriate function to it

void Start ()
    {
        gameFailedEvent += StartCoroutine;
    }

It works when it is called from the same script like so:

gameFailedEvent(WaitThenFailLevel());

when this WaitThenFailLevel() looks like this:

IEnumerator WaitThenFailLevel()
    {
        CharacterController2D.playerDied = true;
        if (CharacterController2D.animState != CharacterController2D.CharAnimStates.fall)
        {
            CharacterController2D.currentImageIndex = 0;
            CharacterController2D.animState = CharacterController2D.CharAnimStates.fall;
        }
        CharacterController2D.movementDisabled = true;
        yield return new WaitForSeconds(0.2f);
        StartCoroutine(ScaleTime(1.0f, 0.0f, 1.2f));
    }

It works fine here. Now, I have another object that can kill the player (dangerous times I know) and I don't want to copy paste everything again, I just want it to fire off the static event made in the script above.

I DID try making the WaitThenFailGame function

public static

and make static all my other ienumerators but I got an error named "An object reference is required for non-static field..." Hence I tried the event stuff. All well and fine, but I can't call the event from the other script because I can't pass it the function from the script mentioned. What to do now?

Upvotes: 1

Views: 1407

Answers (2)

Ahmed I. Elsayed
Ahmed I. Elsayed

Reputation: 2130

using UnityEngine;
public class ScriptA : MonoBehaviour
{
  public ScriptB anyName;


  void Update()
  {
    anyName.DoSomething();
  }
}

using UnityEngine;
public class ScriptB : MonoBehaviour
{
  public void DoSomething()
  {
     Debug.Log("Hi there");
  }
}

This is linking functions between scripts , Copied from Here, maybe coroutines are the same, Then you need to start the coroutine in void Start() {} , You may find this useful as well.

Upvotes: 1

Umair M
Umair M

Reputation: 10720

Here is the example code:

EventContainor.cs

public class EventContainer : MonoBehaviour
{
    public static event Action<string> OnGameFailedEvent;

    void Update()
    {
        if (Input.GetKey(KeyCode.R))
        {
            // fire the game failed event when user press R.
            if(OnGameFailedEvent = null)
                OnGameFailedEvent("some value");
        }

    }
}

Listener.cs

public class Listener : MonoBehaviour
{

    void Awake()
    {
        EventContainer.OnGameFailedEvent += EventContainer_OnGameFailedEvent;
    }

    void EventContainer_OnGameFailedEvent (string value)
    {
        StartCoroutine(MyCoroutine(value));
    }

    IEnumerator MyCoroutine(string someParam)
    {
        yield return new WaitForSeconds(5);

        Debug.Log(someParam);
    }

}

Upvotes: 1

Related Questions