- Home /
PlayerInput - SendMessages not working after changing the scene
I instantiated two prefabs with PlayerInput attached using a PlayerInputManager. In the first scene the PlayerInput-Scripts worked fine with my own scripts (lets call them scripts a). The Behavior of PlayerInput is set to SendMessages. The Gameobject with the PlayerInputManager and both PlayerInput-Prefabs are set to DontDestroyOnLoad. When Scene 2 begins i replaced scripts a with scripts b wich also should receive the SendMessages by PlayerInput, but they dont. I also tried it without replacing but it doesnt work with script a too. Does someone know the reason why?
Hey, I'm having a similar issue. OnControlsChanged is not being called when I change the scene. Have you found a solution or is this just a bug?
Answer by djexstas9 · Jan 13, 2021 at 06:20 PM
May be some problems if you don't let the garbage collector destroy old objects and then using smth like "Find object of type" or any other Find functions which can return you reference to old object from memory instead of new one.
I've suffered this behaviuor when made static C# events and didn't unsubscribe from them or set the reference to MonoBehaviour or Component or GameObject to static field and not set it to null when scene reloads. Since these times I almost never use static refs to objects.
Also if you not cleaning up references from your manager to objects which should be destroyed it can behave like that because you don't let GC collect these objects and they're still in memory.
And offtop: Consider avoiding send messages because it is very hard to debug and very easy to forget to change method name string if you renamed a method. It's very hard to read and hard to find references to a method in your ide.
You can replace this with c# interfaces or with making a base class for everything you need to be called by one call (but interfaces better) or using UnityEvents to call your methods through editor reference to needed method or make a small script that will call all you need when you need but $$anonymous$$essages are very very thing you should avoid.
I have a question, @djexstas9 , how can you (on the JS side after building in WebGL) go past this error: SendMessage: object Object1 does not have receiver for function moveObject
Please check out this question: https://answers.unity.com/questions/1838034/unity-webgl-unityinstancesendmessage-error-sendmes.html