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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to handle unused references in various scripts.

Discussion in 'Editor & General Support' started by Jaccohero, Jan 13, 2023.

  1. Jaccohero

    Jaccohero

    Joined:
    Aug 31, 2022
    Posts:
    3
    Hi all,

    So for me, new Unity user, started working on my first game. Also first post here :(;).

    I have a scene with a gamemanager script. This script has a few Public or SerializedFields and references to other scripts in it. This is for scene X.

    For scene Y I need just a few of these GameManager fields, for the unused fields I do not have the actual gameobjects. When I start scene Y, I get the error: "UnassignedReferenceException: The variable player of GameManager has not been assigned."

    I get the error because the references to these fields are not in my scene Y as they are in scene X.
    So short question... how do I handle unused references in various scenes?

    Please help.
    Thanks in advance.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Two options:

    A: Write a different game manager that only cares about the smaller scope in this scene.

    B: If all these unassigned fields are Unity objects, simply null check them (
    if (someObject == null)
    ), and if not present, don't do the thing you normally do if they were present.
     
  3. Jaccohero

    Jaccohero

    Joined:
    Aug 31, 2022
    Posts:
    3
    Hi @spiney199,

    Thanks for your quick response.
    Ill try to apply these and let you know what the outcome will be.
    thanks.