Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calling a Method Using Game Event Scriptable Object

Discussion in 'Scripting' started by Fender001, Dec 20, 2019.

  1. Fender001

    Fender001

    Joined:
    Mar 15, 2015
    Posts:
    3
    I'm trying to use a scriptable object Game Event and Game Event Listener to call keep dependencies low and things more modular. How it's formatted right now is I'm using a UnityEvent startScene, which OnTriggerEnter() is invoked. Here's That Code:
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2. {
    3.     //if player
    4.     if(other.tag == "Player")
    5.     {
    6.         //raise
    7.         startScene.Invoke();
    8.         Debug.Log("Invoked");
    9.  
    10.     }
    11. }



    Then this is the event in editor. From here, I am using my own Game Event, and "raising" it as a response to Start Scene(). I then have a Game Event Listener elsewhere in my scene that looks like this:



    I believe this should be triggered on the raising of that Game Event PlayCutscene, and then call to my MonoBehavior CutsceneManager, specifically .startNextCutscene():

    Code (CSharp):
    1.  //Starts next cutscene
    2. public void startNextCutscene()
    3. {
    4.     Debug.Log("Signal Received");
    5.  
    6.     //Starts cutscene
    7.     other.cutsceneStart((listOfScenes[selection]));
    8.     Debug.Log("start?");
    9.     //Increments cutscenes
    10.     selection++;
    11. }
    (so this is sort of a side issue, but "other" is a MonoBehavior class that I created, and I just needed a way to call a specific method from it, so I declared other as an instance of that class and then made a call on it to give the method a value from my List of PlayableDirectors. Like I said, this is a side issue but any advice would be greatly appreciated.)

    but this is my bug: I'll get the Debug "Invoked" but not the "Signal Received".

    Any help, tips, or advice would be really greatly appreciated, thank you for any time you put in to help me here in advance!

     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Fender001

    You don't show all your code and the explanation is a bit hard to follow... but the issue I see is I don't see where you subscribe your listener to listen the target event.

    You should have something like this - what you already actually have in your event trigger:

    myEvent.Invoke();


    Then in your scene object that listens to this event:
    Code (CSharp):
    1. public class EventListener : MonoBehaviour
    2. {
    3.     public CodeInSO soContainer;
    4.  
    5.     void Start()
    6.     {
    7.         Debug.Log("Added listener");
    8.         FindObjectOfType<EventTrigger>().myEvent.AddListener(soContainer.DoSomething);
    9.     }
    10. }
    Or you can manually drag the event listener to your event trigger (as it is UnityEvent, it will have a UI to add listener methods to), and set the called method there. Doesn't matter if you do it like this or not.

    It doesn't matter either, if the script run is in ScriptableObject like here, or in MonoBehaviour, as long as the SO is linked somehow to a script that is in scene.
     
    Last edited: Dec 20, 2019
    Fender001 likes this.
  3. Fender001

    Fender001

    Joined:
    Mar 15, 2015
    Posts:
    3
    Thank you very much!