Spawn menu on Level Load

I have created two scenes - menu '1' and level '2'.

Menu has two buttons connected to relaed empty gameobjects (spawnpoints locations).

Level has player inside.

Both buttons and spawnpoints travel across scenes.

  • My problem is: Whitchever button has been pushed, player transfers himself only to one location.

Code inside buttons:

DontDestroyOnLoad (transform.gameObject);
var sp : GameObject;
function OnMouseUp () 

function OnLevelWasLoaded (level : int) 
  if (level == 2)
  var gracz = GameObject.FindWithTag("Player");
  gracz.transform.position = sp.transform.position;
  gracz.transform.rotation = sp.transform.rotation;
  renderer.enabled = false;

Doublecheck in the Inspector that the sp variable on each button is set to a different spawn point. It sounds like they are both referencing the same spawn point.

The problem is that regardless of which button is pressed, a new level is loaded. The code that actually moves the player is inside OnLevelWasLoaded. All OnLevelWasLoaded methods will be run for any active objects in the scene. So what's happening is, both buttons are moving the player when the level changes. Whichever script runs last "wins".

Here are two ways to solve this (many others exist.)

If the player object exists in the original scene (and is not destroyed by the level load) then you could move it before loading the new level.

Otherwise, you could record which button was pressed. Add this to the top of the button script:

var fButtonPressed = false;

Then change the OnLevelWasLoaded method:

function OnLevelWasLoaded (level : int) 
  if (level == 2 && fButtonPressed)
// etc

My Javascript syntax may be slightly off but that's the idea.

Yeah. Got it! Thanks