1

I have a prefab Animal with attached script Animal.cs. There I'm trying to fire an event, which I want to handle in another script.

I'm trying two different approaches - and the code for that is like so:

[System.Serializable] public class _UnityEventFloat:UnityEvent<float> {}

public class Animal : MonoBehaviour
{
    [SerializeField]
    UnityEvent onAnimalKill;
    [SerializeField]
    _UnityEventFloat whenAnimalIsKilled;
    ...
    void OnMouseDown()
    {
        onAnimalKill.Invoke();
        whenAnimalIsKilled.Invoke(10f)
    }
}

And then I have an empty global GameObject with attached script - GameController.cs. There I'm trying to catch the events fired - like this:

public class GameController : MonoBehaviour
{
    ...
    public void OnSomeAnimalKill()
    {
        Debug.Log("ANIMAL WAS KILLED!!!");
    }
    public void OnSomeAnimalKillWithScore(float score)
    {
        Debug.Log("ANIMAL WAS KILLED - WITH SCORE: " + score);
    }
}

However - I can't connect the two scripts. I'm trying to drag the methods from GameController on the slots of the Animal - but it doesn't work:

enter image description here

enter image description here

Any idea what I'm doing wrong? ... I guess it's something super simple - but I can't figure it out.

3
  • Which Unity version are you using? There was a bug in an older one where every UnityEvent was missing the dynamic callbacks you are looking for Commented Jul 27, 2021 at 17:01
  • Does this answer your question: i.postimg.cc/Y23VNtwY/image.png ... And also - I'm on Linux, don't know if this has anything to do with the problem :) Commented Jul 27, 2021 at 17:13
  • Thank you, @derHugo, for the help - but I just tried some things, and when I drag the prefab onto the scene - then I can successfully use GameController methods onto the UnityEvent. But the thing is that my GameController dynamically creates those objects - by instantiating them. And then this doesn't work - as I've shown on the screenshots. So - do you know a way to do the same but with dynamically created objects? Commented Jul 30, 2021 at 14:43

1 Answer 1

1

From your comments we now know:

You are dragging in the GameController Script Asset into the slots of Animal Prefabs!

Two problems with that:

  • You don't want to reference the script asset itself but rather an instance of it attached to an existing GameObject
  • You can't have any Scene references in Prefabs anyway

What you rather want to do is right where you instantiate your instances there assign the callback dynamically like e.g. (we don't have your exact code)

[SerializeField] private Animal prefab;

and then

var animal = Instantiate (prefab);
animal.onAnimalKill.AddCallback(OnSomeAnimalKill);
animal.whenAnimalIsKilled.AddCallback(OnSomeAnimalKillWithScore);

Note: you will not see these dynamically added callbacks in the UnityEvent in the Inspector.


Or what I would actually suggest would be not using UnityEvent at all since you can't serialize the callbacks anyway but rather e.g.

public static event Action onAnimalKilled;
public static event Action<float> whenAnimalKilled;

and later invoke them like

onAnimalKilled?.Invoke();
whenAnimalKilled?.Invoke(10f);

And then all you ever need to do in the Gamecontroller is

private void Awake ()
{
    Animal.onAnimalKilled += OnSomeAnimalKill;
    Animal.whenAnimalKilled += OnSomeAnimalKillWithScore;
}

private void OnDestroy ()
{
    Animal.onAnimalKilled -= OnSomeAnimalKill;
    Animal.whenAnimalKilled -= OnSomeAnimalKillWithScore;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Aah ... You can't have any Scene references in Prefabs anyway is actually my initial problem :) ... I'm new to Unity so this is something I didn't know. Thank you for the help and the answer, man :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.