Changing scenes

got scripts? Need scripts?
Post Reply
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Changing scenes

Post by SpiritWebb »

Alright, I can move from one scene to another just fine, but when I return to my test scene from which I started, it creates another player, camera, minimap cam, etc. These were placed in the main scene when starting. How do I prevent this from happening?

This is my current code in Javascript:

Code: Select all

var availableLevels : String[];
var PlayerObject : GameObject;
var minicam : GameObject;
var maincam : GameObject;
var playerSpawn : String;

var windowWidth : int = 200;
var windowHeight : int = 200;
private var showMenu : boolean = false;
var playerController : SpaceFlight;

function OnMouseDown () {  
    showMenu = true;
    playerController.moving();
           
}

function OnGUI () {

    if (showMenu) {
    	
         GUI.Window(0, 
                   Rect((Screen.width / 2) - (windowWidth / 2), 
                        (Screen.height / 2) - (windowHeight / 2), 
                        windowWidth, 
                        windowHeight), 
                   LevelSelect, 
                   "3-1 Phase Gate");  //Creates a window with ID 0 in the center of the screen using the function LevelSelect with the title, "Select a level!"
    }
}

function LevelSelect (id : int) {
    for (var levelName : String in availableLevels) {
        if (GUILayout.Button(levelName)) {
            DontDestroyOnLoad (PlayerObject);
            DontDestroyOnLoad (minicam);
            DontDestroyOnLoad (maincam);
            Application.LoadLevel(levelName);
            spawnPoint = GameObject.FindWithTag(playerSpawn);

        }
    }
    if (GUILayout.Button("Cancel")) {  //Gives the player the opportunity to back out of the menu.
        showMenu = false;
    }
}
Image

Image
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Changing scenes

Post by hallsofvallhalla »

Is your first level test level? as in when you first start the game? You may need to put a if(playerobject exists) then do not create type of function in your player creation in the level load. Hard to tell without seeing all of it. Sounds to me though either you have a copy of the prefab in the level already or it is creating the player every time test loads through a function.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Changing scenes

Post by SpiritWebb »

It doesnt destroy the objects so they can pass through! I put the player, main cam and minicam down in level 3-5 for testing, put a DontDestroyOnLoad(names here) in the script (see above) and passing into 3-1 it worked, however going back to 3-5 the player, cam and minicam are already in the scene, and doesn't destroy the one I am playing, and receive 2 of everything.

Looking for a good spawn script that will spawn the player, cam and minicam. That way when I hit play, if it doesn't exist it will create it!!
Image

Image
Post Reply

Return to “Scripting/Coding”