Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved PlayerScript Update not being called after SceneLoad

Discussion in 'Scripting' started by antonioniii, Oct 4, 2023.

  1. antonioniii

    antonioniii

    Joined:
    Jun 15, 2020
    Posts:
    27
    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
     
    Last edited: Oct 5, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

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

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    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:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    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.

    Code (csharp):
    1. public static MyClass Instance { get; private set; }
    2.  
    3. void OnEnable()
    4. {
    5.   Instance = this;
    6. }
    7. void OnDisable()
    8. {
    9.   Instance = null;     // keep everybody honest when we're not around
    10. }
    Anyone can get at it via
    MyClass.Instance.
    , but only while it exists.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    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.
     
  4. antonioniii

    antonioniii

    Joined:
    Jun 15, 2020
    Posts:
    27
    Fixed it. Was feverish when I was posting this last night.
     
    MelvMay likes this.
  5. antonioniii

    antonioniii

    Joined:
    Jun 15, 2020
    Posts:
    27
    Thank you. I'll look into all of this shortly. Appreciate the response and help.
     
  6. antonioniii

    antonioniii

    Joined:
    Jun 15, 2020
    Posts:
    27
    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
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,843
    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.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    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.
     
    antonioniii and MelvMay like this.
  9. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    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...)
     
    antonioniii and Bunny83 like this.
  10. antonioniii

    antonioniii

    Joined:
    Jun 15, 2020
    Posts:
    27
    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
     
    Nad_B likes this.
  11. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Don't be embarrassed, we all have been there.