- Home /
Place holder for various scripts/functions
So, I have just finished making dialogue boxes in my game, and for this first dialogue box, I need it to be so as soon as the dialogue box goes away (SetActive (false)), it brings up a menu, but for some reason I can't figure out how to do that. I've done lots of similar things with other menus, but the problem is thus: As there is need for this sort of thing in multiple places for different reasons, I'd like to have functionality so that when any dialogue box is SetActive (false) in the game, I have a way to call various functions from various scripts, kind of like the button UI inspector thing shown below: But I can't think of how to do that! I looked into Event Triggers, but it doesn't have any events for disabling stuff... or enabling, for that matter.
At the very least, I'd like if I could just make this one box call something when I set it inactive.
Thanks, guys!
Answer by Hellium · Sep 18, 2017 at 01:10 PM
You will have to attach the following script to your panels so as to dispatch an event :
[System.Serializable]
public class SimpleEvent : UnityEvent { };
public class MyClass : MonoBehaviour
{
public SimpleEvent OnGameObjectDisabled ;
void OnDisable() {
if( OnGameObjectDisabled != null )
OnGameObjectDisabled .Invoke();
}
}
Thanks!
However, kind of a noob. I added the "using UnityEngine.Events;" at the top. Now I tried to add it to my panel, and I got this: How do I get around that...?
The class in his example is named "$$anonymous$$yClass", not SimpleEvent. So your file should be named $$anonymous$$yClass. SimpleEvent is just a custom implementation of a UnityEvent (which actually isn't necessary when you use an event without parameters).
Answer by Bunny83 · Sep 18, 2017 at 01:21 PM
You could use a component like this:
//TriggerOnEnableChange.cs
using UnityEngine;
using UnityEngine.Events;
public class TriggerOnEnableChange : MonoBehaviour
{
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public UnityEvent onEnable;
public UnityEvent onDisable;
public BoolEvent onToggle;
private void OnEnable()
{
onEnable.Invoke();
onToggle.Invoke(true);
}
private void OnDisable()
{
onDisable.Invoke();
onToggle.Invoke(false);
}
}
Now you can assign a simple method to the onEnable or onDisable events. The onToggle event takes a bool parameter of the new state and is called both when the object is enabled or disabled.
Oh! I didn't look closely enough at this! I will try it right away and let you know!
Thanks!