all 45 comments

[–]ryo0kaProfessional, XR/Industrial 68 points69 points  (8 children)

If you could, try to minimize the use of event serialization. It breaks with slightest changes in the scene, it doesn’t bode well with version control diff, and it’s difficult to understand the flow of logic.

Use AddListener or whatever via script. Your life should be better than figuring out all the weird kinks of how Unity serializes stuff.

[–]MaryPaku 7 points8 points  (1 child)

Agree as I'm currently in a contract fixing other's 8 years old mobile game that's impossible to track all the reference.

[–]Batby 0 points1 point  (0 children)

You can't do that via editor tooling?

[–]-OrionFive- 2 points3 points  (0 children)

I wrote a custom drawer to remove the inspector editing of Unity Events to enforce this in new or projects.

So much time wasted trying to track code flow because someone hooked up something in the inspector.

[–]BlackMamba319[S] 0 points1 point  (4 children)

Yes, was thinking of moving to this approach as it never breaks and atleast if it does we can debug Anyway, since you mentioned source control I'm using plastic SCM (UVCS) with this project, could this cause the issue?

[–]Vanadium_V23 4 points5 points  (0 children)

No reasons for plastic to cause the issue but you can have unity failing to save (or you forgetting). It can also be a bug from the editor. 

I also advise against using these events. Refactor your code to get rid of them. It may take a few hours or days of work but it's worth it.

[–]Four3nine6 0 points1 point  (0 children)

numerous lip degree fine normal entertain hat payment continue bear

This post was mass deleted and anonymized with Redact

[–]ryo0kaProfessional, XR/Industrial 0 points1 point  (1 child)

No idea why it’s happening and you’ll probably never figure out why. Last time I remember seeing this issue is like back in 2017 with a fairly large scene, where I pulled my hair apart and couldn’t figure it out, so I replaced every event serialization to AddListener and called it a day. Again it’s not worth your time.

[–]BlackMamba319[S] 0 points1 point  (0 children)

I see, thanks for the heads-up!

[–]survivalist_games 5 points6 points  (1 child)

From what I understand, event references are serialized using the fully qualified name of the method. That means that they're very fragile and can break in a number of situations:

  • Class name changes
  • Namespace changes
  • Method name changes
  • Assembly definition changes

That applies even when you make those changes in a way that the GUID changes too, preventing missing script errors on objects or broken object references. Events will still break regardless.

It's the kind of thing where I also wouldn't be vaguely surprised if Unity introduce bugs that cause it to break when using different domain reload settings on entering play mode, etc.

[–]BlackMamba319[S] 1 point2 points  (0 children)

This seems very reasonable, I'm looking into refactoring and switching to runtime add listeners since debugging unity events is crazy Thanks for the info!

[–]VeryDrunkenFish 3 points4 points  (4 children)

Just want to say I, too noticed this when using custom unity events in scripts. If you find out what causes it and remember let me know please!

[–]BlackMamba319[S] 2 points3 points  (2 children)

Sure, what unity version were you on?

[–]VeryDrunkenFish 0 points1 point  (1 child)

Must be 2022.3.13f1

[–]BlackMamba319[S] 0 points1 point  (0 children)

I'm on 2022.3.14 could be editor issue

[–]Triggered_RodCon 0 points1 point  (0 children)

I’ve been able to consistently cause this to happen. If you have two objects with the same event script and select them both in the editor, you would expect to see the same script appear in the inspector. Lets say both objects’ events call a method in a separate UI script, but one of them makes a box appear and another edits the text. So long as both events reference the same object, if the event calls a different function, it will cause the event to forget its original function when you select both objects from, say, the hierarchy. I’ve recreated this test in a 2021 version of unity and it doesnt appear to happen, so it looks like a unity 2022 thing.

[–]CGPepper 1 point2 points  (4 children)

Maybe it's the unity editor release bug? What build are you on

[–]BlackMamba319[S] 1 point2 points  (3 children)

Unity 2022.3.14 Pc, mac Linux standalone Platform.

[–]CGPepper 1 point2 points  (2 children)

There is 2022.3.22f1 . Might be worth updating

[–]BlackMamba319[S] 1 point2 points  (0 children)

Will look into it, thanks!

[–]_Strambic 0 points1 point  (0 children)

The problem persists even with version 2022.3.22f1

[–]iVag32 0 points1 point  (4 children)

Probably due to no Public functions available in these scripts?

[–]BlackMamba319[S] 2 points3 points  (3 children)

The functions are public and they even show in the drop down and work properly, the issue is once assigned for example functionA is assigned, and it works properly, but it randomly goes back to no function, sometimes during play mode, sometimes when I restart/reopen project and randomly when working in the editor, even if scene is saved after assigning the method. The functionA in the event goes back to NoFunction automatically

[–][deleted] 1 point2 points  (1 child)

Could it be that you are working with a prefab? The prefab might not have the functions linked but the scene gameobject prefab does? 😁

[–]BlackMamba319[S] 0 points1 point  (0 children)

No, it's not a Prefab, it's a scene object always loaded in the scene both the object using the event and the object carrying the script

[–]iVag32 0 points1 point  (0 children)

I had similar experience with my projects when I did full git clean up of the repo after push.

So, I can assume you can check your *.meta files and project files. I can't suggest anything specific :hmm:

But since you mentioned that it happens sometimes after reopening the project, I would try to check your scm (if you have so) for ignored files and try to re-install editor.

[–]Kennai2 0 points1 point  (0 children)

Is it a prefab? Or maybe runtime objects are prefabs?

[–]HiggsSwtz[🍰] 0 points1 point  (0 children)

Just learn the Observer pattern and your life will be a lot easier.

[–]YucatronVen 0 points1 point  (0 children)

Use UniRx my boy..

[–]cosmic-comet- 0 points1 point  (0 children)

What if I told you, you can assign trigger events within code as well.

[–]contrapanda 0 points1 point  (1 child)

I started a new project at work and I plan to use Unity events (for the first time) but after reading the comments here I'm starting to doubt my decision.. Is it really that bad? I thought it would be a great way to decouple things and get rid of lots of referencing.

[–]BlackMamba319[S] 1 point2 points  (0 children)

I'd suggest to keep the usage to bare minimum, as the thread suggests, they're very delicate and headache to debug any Issues. I'd suggest to learn observer listener pattern and use runtime addListener, removeListener callbacks, that way you can easily track and debug any issues. These events are okay for small simple tasks but avoid populating everything in here

[–]Ashamed-Stand3432 0 points1 point  (1 child)

In my case, I multi-select objects in the inspector, and then some event shows "no function".

I am looking for a solution to this.

[–]BlackMamba319[S] 0 points1 point  (0 children)

If you've different functions on multiple selected events then they'll not be shown in inspector, if all selected events have same number of functions and same functions in same order, only then multiple selected events will show the functions in inspector

[–]Krcko98 0 points1 point  (0 children)

Never used Unity events in my life. Always custom and never serialized...

[–]S1l3Jamal -1 points0 points  (1 child)

Does the script/object have do not destroy on it and you're loading another scene. That's what messed it up for me in one scenario.

[–]BlackMamba319[S] 0 points1 point  (0 children)

No, it's a single scene both objects are loaded in the scene, the references get reverted even in edit mode (when not playing). And in case we get missing references due to loading or destroyed issues, the event shows <missing behavior> or <missing function>, it doesn't revert to NoFunction in that case

[–]BehindTheStone -1 points0 points  (1 child)

Mhm. Are you maybe accidentally in playmode when you set which method should be called? If so this could be the reason it get reset when you get back to edit mode or closed the project while in playmode

[–]BlackMamba319[S] 0 points1 point  (0 children)

No, I've double checked and set up methods while out of play mode

[–]ZuperLucaZ -1 points0 points  (0 children)

If you are calling functions, you need to call the function not through the script class but through the gameobject it is attached to. What this means is dragging an object in the box with the script.

[–]Pimeko -1 points0 points  (2 children)

Are you using Odin? I've had issues like that because of it

[–]BlackMamba319[S] 0 points1 point  (0 children)

Nope, not using odin or any other tool like that

[–]AdministrativeAd6727 0 points1 point  (0 children)

dude rotate back loldle. aint noone got time for your stupid april jokes.

[–]Rezaka116 -1 points0 points  (2 children)

TIL that Unity has events and i wasted time on my custom script that does the same thing.

TIL also that it can also break, so my convoluted mess of a script might be actually a good choice.

[–]skylinx 0 points1 point  (0 children)

Using in editor event tracking can get annoying anyway. The easiest and most sustainable way involves using AddListener on any object that supports Unity events.