最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unity 6, Scriptable Object Event System Doesn't Work After Restarting Unity editor - Stack Overflow

programmeradmin0浏览0评论

As the Title suggests the script works fine when I set it up in the first place.

It's the exact same script from the tutorials found on YouTube discussing the Scriptable Object based Event System. I set it up correctly, and it works as expected. In order to fix the issue when I start Unity again (the next day or something), I have to duplicate the Game Objects, with the associated Listener Scripts, and everything works. I save the project, the scene, close down Unity, reopen Unity and the Listeners stop working.

I have deleted the Libraries, I have deleted Preferences, I have deleted, or reset just about everything I could think of. I even deleted the C# scripts themselves and rewrote the same exact code. That allows it to work for a few days, but ultimately it DOES result in this issue happening sooner or later.

What is it that is happening?

using UnityEngine;
using UnityEngine.Events;

public class GameEventListener : Activation
{
    [SerializeField] GameEventAsset gameEvent;
    [SerializeField] UnityEvent onEventTriggered;

    public GameEventAsset GameEvent { get { return gameEvent; } set { gameEvent = value; } }
    public UnityEvent OnEventTriggered { get { return onEventTriggered; } set { onEventTriggered = value; } }

    private void OnEnable()
    {
        Debug.Log("OnEnable Event");
        gameEvent.AddListener(this);
    }

    private void OnDisable()
    {
        gameEvent.RemoveListener(this);
        onEventTriggered = null;
    }

    public void OnEventRaised()
    {
        Debug.Log("OnEventRaised");
        onEventTriggered.Invoke();
    }
}

and here is the Scriptable Object Code:

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "ScriptableObjects/GameComponent/Event/This is GameDev SO Event System")]
public class GameEventAsset : ScriptableObject
{
    [SerializeField] List<GameEventListener> listeners = new List<GameEventListener>();
    public void AddListener(GameEventListener listener)
    {
        Debug.Log(listener);

        if (listener != listeners.Contains(listener))
        {
            Debug.Log(listener + " Is listener");

            listeners.Add(listener); 
        }
    }

    public void RemoveListener(GameEventListener listener)
    {
        listeners.Remove(listener);
    }

    public void TriggerEvent() 
    {
        Debug.Log(listeners.Count + " Trigger Event Start");

        for (int i = 0; i < listeners.Count; i++) 
        {
            Debug.Log(listeners[i] + " Trigger Event Engaged");

            listeners[i].OnEventRaised(); 
        }
    }
}

I added Debug.Log throughout the SO's thinking that they were the issue, but it is the Listener scripts that aren't firing. I see my logs SHOW that the events ARE getting triggered, but they are NOT firing in the Listener Script... when I close and reopen Unity after the initial test.

I am at my wits end with this and am unable to figure out how to fix it. I even downloaded the latest version of Unity: 6000.0.43f1, and was originally using 6000.0.39f1. Both give me the same issues.

I want to reiterate, the issue is that the code does not work when I close and reopen Unity. It 100% works as expected if I make duplicates of the existing Game Objects, but only for as long as that instance of Unity is open. If I save, and save project close Unity, then reopen unity it stops working. I am assuming there is a setting, or corrupted something or other that is the issue.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论