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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question ScriptableObject with C# event not working in unity editor, but working in build, why?

Discussion in 'Scripting' started by brusrecenthusky, Apr 27, 2022.

  1. brusrecenthusky

    brusrecenthusky

    Joined:
    Mar 1, 2019
    Posts:
    3
    Unity version is 2021.3.0f1 LTS. I have a Scriptable Object that contains a c# event and a couple methods to add, remove, and clear listeners. Yesterday, the project worked as intended in the unity editor where if the c# event was invoked, the listeners would then execute, but now, whenever I try to execute it, nothing happens. So I tried creating a build to see if it would work there, and it does. Any explanation to this? Are c# events not very compatible with scriptable objects? This is my code:
    Code (CSharp):
    1.  public class RSEvent : ScriptableObject
    2.          {
    3.              #region Simple
    4.    
    5.              private event Action _simpleEvent;
    6.    
    7.              public void Invoke()
    8.              {
    9.                  _simpleEvent?.Invoke();
    10.              }
    11.    
    12.              public void AddListener(params Action[] listeners)
    13.              {
    14.                  foreach (Action listener in listeners)
    15.                  {
    16.                      _simpleEvent += listener;
    17.                  }
    18.              }
    19.    
    20.              public void RemoveListener(params Action[] listeners)
    21.              {
    22.                  foreach (Action listener in listeners)
    23.                  {
    24.                      _simpleEvent -= listener;
    25.                  }
    26.              }
    27.    
    28.              #endregion
    29.    
    30.              #region Complex
    31.    
    32.              private event Action<object> _complexEvent;
    33.    
    34.              public void Invoke(object args)
    35.              {
    36.                  _complexEvent?.Invoke(args);
    37.              }
    38.    
    39.              public void AddListener(params Action<object>[] listeners)
    40.              {
    41.                  foreach (Action<object> listener in listeners)
    42.                  {
    43.                      _complexEvent += listener;
    44.                  }
    45.              }
    46.    
    47.              public void RemoveListener(params Action<object>[] listeners)
    48.              {
    49.                  foreach (Action<object> listener in listeners)
    50.                  {
    51.                      _complexEvent -= listener;
    52.                  }
    53.              }
    54.    
    55.              #endregion
    56.          }
     
  2. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    508
    Do you mean in editor during play mode or actual editor code like editor windows?

    They work fine during play mode, there might be a editor specific bug in your other code?
     
    Last edited: Apr 27, 2022
  3. brusrecenthusky

    brusrecenthusky

    Joined:
    Mar 1, 2019
    Posts:
    3
    I meant in the editor during play mode. I have the scriptable object hooked up to several mono behaviours and it should work in theory, and it does when deployed in a build, but during play mode the event does not invoke. Yesterday before going to sleep, this problem was not present. Now today, after opening my project, without changing anything, it no longer works as intended. And just for the record, I have tried restarting my project several times to no improvement.
     
  4. brusrecenthusky

    brusrecenthusky

    Joined:
    Mar 1, 2019
    Posts:
    3
    Update: Made a new build, now it doesn't work at all.
    To give a better idea, the process flow of invoking the scriptable object event in the project goes like this, Unity.UI Button Unity Event executes a method that toggles between two enum values, the scriptable object event is then invoked from within the method. The mono behaviors that will observe the event simply have to reference the scriptable object and use the AddListener method.

    I have checked the GetInvocationList of the event and it displays the correct amount, but the events are not invoked.
     
  5. Gold_7

    Gold_7

    Joined:
    Jul 12, 2022
    Posts:
    9
    Hi
    ich don't know if you are still into this project but I kinda have the same problem I have a scriptable object to manage my skins so each of them has a boolean wich says wheter they are bought or not. So when I run my game with the scriptable object opened in the Inspector I can see when the variables change and everything works, but as soon as I don't have it opend in the Inspector the variables don't get updeted why ever