#Singleton managers get destroyed when using InputSystem functions

1 messages · Page 1 of 1 (latest)

bleak sleet
#

Trying to add rumble to my game by literally just doing this:

using UnityEngine.InputSystem

public class CameraManager : MonoBehaviour

public static CameraManager Instance {get; private set;}

void Awake()
{
  Instance = this;
}

void ShakeAllCameras()
{
  //........ blah blah blah
  Gamepad.current?.SetMotorSpeeds(_number, _number);
}

but when ShakeAllCameras() gets called, I get a MissingReferenceException on the CameraManager instance, which is confusing, since the instance should not get destroyed or deleted at all. (this script works fine without the aformentioned additions. weirdly, the issue also persists even if I comment out the above code until I restart the unity editor)

very confusing, am I not able to access the InputSystem namespace in a singleton instance? never seen an issue like this before lol

silent aurora
#

is the missingreferenceexception on the Gamepad or on the CameraManager itself?

#

like, where is the stacktrace pointing to

bleak sleet
#

the CameraManager itself

silent aurora
#

that doesn't give me much info

#

what line is the stacktrace pointing to

#

oh wait that was answering my first question lol

#

it's not related to InputSystem then, it's about where you're calling the ShakeAllCameras function

bleak sleet
#

well this is weird, I tried connecting a gamepad and it works now
(even after disconnnecting the gamepad, since I guessed it might only be if gamepad.current is null but it just inexplicably works now)

#

so I can't get that stack trace for you

silent aurora
#

it kinda sounds like the error was actually on the Gamepad?

#

you just fell for the unity null trap

#

hmm wait no, Gamepad isn't a UnityEngine.Object

bleak sleet
#

strange
I just added a check for if the current control scheme is gamepad, maybe it's the null propagation causing issues. hopefully it doesnt come up again

bleak sleet
#

just tried adding a debug.log for something else in cameramanger and it started happening again

marsh fox
#

If you want the CameraManager to exist as one object only, you need to get rid of all objects created after the first one in Awake. Don't know if yours should exist during all scenes (DontDestroyOnLoad), or only in one scene, but "Instance=this;" does not do it.

Here is what my GameManager-Awake looks like.

if (Instance == null) {
 Instance = this;
}
else if (Instance != this)
{
 //enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
 Destroy(gameObject); //<- this makes OnDestroy() be called and we don't want to deinit everything there, be careful
 return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
//...more init code
bleak sleet
#

mine isn't dontdestroyonload so there should only ever be one anyway

#

I've found out this issue was actually caused by an addon I'm using (fast script reload). doing a full domain reload fixes it, and I've reported it to the asset creator