PlayerScript Update not being called after SceneLoad

Hey. Any help would be greatly appreciated. I’ve been trying to debug this issue for hours now. My playerscript on my player gameobject works fine until a scene change. This wasn’t an issue a couple of days ago… I have no idea why it’s occurring. This player script is on my player object which is a prefab in both scenes.

It seems like some variable within the PlayerScript are functioning (like the screen edge coordinates) but my Debug isn’t being called (and all of my functions in the Update thread). Please see my video below of the issue and I’ve included my GameManager and PlayerScript.

Youtube video of the issue - Removed for privacy

You can see here in the video, the Debug in the PlayerScript is running fine on the opening scene. But once we change scenes, the debug no longer is being called…

Removed scripts for privacy

Some red flags are all of the various static things you have going:

  • the static event you have in the first script
  • the static instance thing you have (is that even used?)
  • your subscription to the OnSceneChanged event… I don’t see you unsubscribing in an OnDisable()

Eliminate all the statics and ensure your subs match your unsubs.

ALSO, generally do not drag managers into any scene, EVER. It’s always a source of problems Try this approach instead:

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance

Alternately you could start one up with a RuntimeInitializeOnLoad attribute.

The above solutions can be modified to additively load a scene instead, BUT scenes do not load until end of frame, which means your static factory cannot return the instance that will be in the to-be-loaded scene. This is a minor limitation that is simple to work around.

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.

And finally there’s always just a simple “static locator” pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.

WARNING: this does NOT control their uniqueness.

WARNING: this does NOT control their lifecycle.

public static MyClass Instance { get; private set; }

void OnEnable()
{
  Instance = this;
}
void OnDisable()
{
  Instance = null;     // keep everybody honest when we're not around
}

Anyone can get at it via MyClass.Instance., but only while it exists.

Please look at your own posts after you make them and edit appropriately. Your code has underlining on it all which makes it very difficult to read.

Fixed it. Was feverish when I was posting this last night.

Thank you. I’ll look into all of this shortly. Appreciate the response and help.

Solved. There was a null reference in my playerscript which caused the script to stopped being called FYI. I ended up working with a codementor to adjust a lot of my game structure as well

It should go without saying that runtime exceptions should not be ignored. You should always have the console open and visible, and investigate any errors that occur from within your own code.

I like to quote Ryan Hipple on the case of debugging:

“Never fix a bug you don’t understand”

This is the only way how you actually learn and understand what you’re actually doing. If something unexpected happens, it’s your duty to figure out what it is, understand why it happened and then fix it. Bugs and issues are partly based on oversight errors (copy and paste issues, mistyping, missing setup / reference) and on the hand based on general misunderstanding of an aspect of the system. That second part is which is important to improve, so you can build confidence in your understanding how it actually works. It makes finding the first part issues much easier.

This ^

Lot of (inexperienced) Unity developers just ignores exceptions because “the game is still running!” but fail to understand that any non caught exception halts the execution of the current method abruptly, leaving the game in an unstable state (for eg. if an exception is thrown during a for loop, the loop just stops and won’t be called for the next item, or if it’s before a raycast test, that raycast won’t be executed…)

Yep, im embarrased that me not monitoring the console was the issue but I figurered its good to post what the issue was to archive other amateurs like myself

Don’t be embarrassed, we all have been there.