- Home /
My UnityEvent doesn't "show the events"
So, I am making a 2D platformer puzzle game and when I touch a trigger, an event will start, but there is nowhere I can assign these events! Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class OnButtonPress : MonoBehaviour
{
public UnityEvent OnPress;
public UnityEvent OnStay;
public UnityEvent OnLeave;
void OnTriggerEnter2D()
{
OnPress.Invoke();
}
void OnTriggerStay2D()
{
OnStay.Invoke();
}
void OnTriggerLeave2D()
{
OnLeave.Invoke();
}
}
Answer by mchts · May 24, 2019 at 12:58 PM
First define your event class and then declare it like below:
[Serializable] public class PressEvent : UnityEvent { }
public PressEvent OnPress;
Now you will see your event in the inspector.
I get some errors (look at the image) Does it still work or am I doing something wrong?
You did it wrong :). Here is the correct way:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class OnButtonPress : $$anonymous$$onoBehaviour
{
[Serializable] public class YourEvent : UnityEvent { }
public YourEvent OnPress;
public YourEvent OnStay;
public YourEvent OnLeave;
void OnTriggerEnter2D()
{
OnPress.Invoke();
}
void OnTriggerStay2D()
{
OnStay.Invoke();
}
void OnTriggerLeave2D()
{
OnLeave.Invoke();
}
}
I still got an error on [Serializable]: The type or namespace name 'SerializableAttribute' could not be found (are you missing a using directive or an assembly reference?) (CS0246) [Assembly-CSharp]
The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?) (CS0246) [Assembly-CSharp]