Subscribing to an event on a gameobject that was moved to the scene wont go through
I've got a problem where I cannot subscribe to an input event that gets fired from a gameObject that has been moved to the current scene from a previous scene, unless the gameObject I'm trying to subscribe from was moved with the aforementioned object.
.
to give a more specific example:
at the start of the game, "load scene" is the starting scene, and it has gameObject with RewiredInputReader(RIR) on it.
I load the scene called "Splash". gameObject with RIR gets moved to "Splash", "Splash" scene gets set active
Then I load "Menu". gameObject with RIR gets moved to load scene first, "Splash" gets unloaded. "Menu" gets loaded, I move gameObject to "Menu" and set "Menu" active
And then I try to subscribe to an event fired from RIR from a script that was already present in "Menu" - lets call this object "ObjectiveHandler".
.
The problem is when I try to do RewiredInputReader.Input_Submit += ClickReceived;
from the ObjectiveHandler that was already present in the "Menu" scene, its script executes everything, but the "subscription" doesnt happen. I can also see RIR firing exceptions for the event firing (ie it detects the submit being received) but the subscribed script on ObjectiveHandler wont receive it.
.
Doing some testing, I found that the sub wont go through even if I sub to RIR in ObjectiveHandler's Update or manually trigger it, not even if I make RIR DontDestroyOnLoad BUT if i place the ObjectiveHandler into the load scene and they get moved together, the sub works consistently.
.
any ideas how I can bridge this problem please? (other than having everything that ever needs to sub to RIR in the load scene) Advice appreciated
Answer by theembracedone · Feb 06 at 03:48 PM
i think i found the culprit. There's another component that is subscribed to "submit" - if I remove the subscription from it, Tester works... but I dont see why that'd prevent the Tester from subbing especially since the other object was doing nothing with the subscription...
Answer by theembracedone · Feb 06 at 02:08 PM
My testing setup is this:
RIR is either moved from scene to scene or is DontDestroyOnLoad
an object called "Tester" with a test script is placed in the load scene to be moved from scene to scene
the same "Tester" object with the test script is placed in the "Menu" scene where I am testing.
The Tester contains this script (where number is just used to differentiate between the two Tester objects): public class Tester : MonoBehaviour { public int number;
void Awake() { Debug.Log("TEST Awake " + number); RewiredInputReader.Input_Submit += ClickReceived2; } public void OnEnable() { Debug.Log("TEST OnEnable " + number); RewiredInputReader.Input_Submit += ClickReceived2; } public void OnDisable() { Debug.Log("TEST OnDisable " + number); RewiredInputReader.Input_Submit -= ClickReceived2; } public void ClickReceived2() { Debug.Log("TEST Click Received" + number); } } }
Upon playing the game in editor, I get the following results:
the Tester that gets moved to "Menu" prints out Click Received fine, but the other one doesnt, it never subscribes, but it prints out the Awake message and also the OnEnable/OnDisable messages just fine, but it will never print out Click Received.