- Home /
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 () { Application.LoadLevel(2); }
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; } }
Answer by Bampf · Nov 05, 2010 at 09:56 PM
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, that is the idea. Correct me if i am wrong: but1 = false but2 = false if level =2 AND but1 is pressed Do smth if else level = 2 AND but2 is pressed Do smth ???
In my proposed solution, each button only knows if it was pressed or not. There are two copies of this script (one for each button). One button script will start to run and realize that it wasn't pressed, and do nothing. The other button will know that it WAS pressed, and move the player.
sorry, I'm missing smth here. I've got 2 buttons with var fButtonPressed = false
The idea wasn't about one button being true and the other false, was it?
Answer by Bampf · Nov 04, 2010 at 07:00 PM
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
random spawn locations.... 2 Answers
How to write a simple save script 1 Answer
How to create a spawn point 1 Answer
[solved] respawn when destroyed 1 Answer