1
\$\begingroup\$

I'm trying to access my enemy and spawning values from two different classes into a class that is trying to change them both. Although when I do this I keep getting a Null Reference Exception error. The error is where spawnManager first gets called.

This is the code that is trying to get the two values. (And just to note I am trying to access these two values from my Welcome scene and these two values are in my Game scene but I do not know if this is affecting anything):

private EnemyAI enemy;
private SpawnManager spawnManager;

void Start() {
    spawnManager = GetComponent<SpawnManager>();
    enemy = GetComponent<EnemyAI>();
}

public void onEasy() {
    spawnManager.enemyShipWait = Random.Range(1f, 3f); <-- This line has the error
    spawnManager.randomPowerupWait = Random.Range(7f, 12f);
    enemy.speed = 2.75f;
}

These are the two pictures of my scenes: Game Scene:

Game Scene

Welcome Scene:

Welcome Scene

\$\endgroup\$
5
  • \$\begingroup\$ Who is calling onEasy() ? \$\endgroup\$ Commented Apr 29, 2019 at 0:13
  • \$\begingroup\$ A button is calling the onEasy() \$\endgroup\$
    – ItsDJJuju
    Commented Apr 29, 2019 at 0:14
  • 1
    \$\begingroup\$ "I am trying to access these two values from my Welcome scene and these two values are in my Game scene" I did not quite understand what you mean here. Can you perhaps post a couple of screenshots from your hierarchy? \$\endgroup\$ Commented Apr 29, 2019 at 0:17
  • \$\begingroup\$ So my project has 2 scenes, a Game scene and a Welcome scene. The two scripts, Enemy AI and SpawnManager, are both in the Game scene while the WelcomeUI, which I showed a preview to above, is in my Welcome scene. I'm not sure if I am able to gather the two scripts in the Game Scene into my Welcome Scene. I will edit the og post to show pictures of the hierarchy, \$\endgroup\$
    – ItsDJJuju
    Commented Apr 29, 2019 at 0:22
  • \$\begingroup\$ Unless the game objects in the game scene are initialized in the welcome scene and persist into the game scene you can't call on them or add references since they have not been loaded up yet. \$\endgroup\$ Commented Apr 29, 2019 at 3:13

1 Answer 1

3
\$\begingroup\$

You can't reference across a scene boundary

If you have two scenes, nothing in scene 1 can reference things in scene 2 and vice versa. When you are in scene 1, scene 2 does not exist and vice versa. You cannot reference things that don't exist and the whole point of scenes is to separate out groups of things that don't need to exist at the same time.

(Or, as I used to say during the Flash Era: "Don't use scenes" but Flash's scene boundaries were much, much harder to pass any data across than Unity's: even the global keyword didn't work!)

  • You can load scenes (combine) when you load a scene, but this just creates a virtual scene with the contents of both (as if you'd only ever had the one scene) and often not what you want (I have seen it used once and that was to separate out a global UI so that two different people could work on separate things without causing merge conflicts).
  • You can call DontDestroyOnLoad() on an object which will cause it to persist across scene loading boundaries, but will cause issues if you return to the scene it originated from (you will get duplicates), but this still doesn't let you reference things that don't exist yet, you'll have to wait for the new scene to be loaded.
  • You can put these values in a globally accessible location (e.g. a static class), but this may create issues as well, but may be what you are looking for in this situation. Remember to make both scripts look at the global values in order to perform their logic.
\$\endgroup\$
1
  • \$\begingroup\$ Okay thank you guys for clarifying that for me \$\endgroup\$
    – ItsDJJuju
    Commented Apr 29, 2019 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .