- Home /
How do I create an effective spawn point script for multiple scenes and change music each scene?
Hi, I'm trying to create spawn points and an automatic music changer, but I can't seem to figure out how to make either one work.
The reason I need spawn points is I've got these portals that teleport the player to each new level, and the portals play a sound effect upon collision with the player. But in order to play the sound effect all the way through, the audio source has to be attached to an object that doesn't get destroyed when the new scene loads. Now I've managed to get that part working fine by setting both the camera and player character to DoNotDestroy (the teleportaion sound effect is attached to an empty game object called "Portal Sound", which is itself attached to the main camera), but now the player character retains his old coordinates from wherever he was standing in the previous scene when the new scene loads, and I need to be able to specify an exact location for him to spawn at in the new scene.
Also I need the music to change automatically when each new scene loads, as each level has its own theme music.
EDIT: Here's the code I've got so far, but it doesn't seem to work:
var SpawnPoint : GameObject;
function OnLevelWasLoaded() { SpawnPoint = GameObject.FindWithTag("SpawnPoint"); transform.position(SpawnPoint.transform); }
Answer by StephanK · Apr 28, 2010 at 07:17 PM
Just attach an empty GameObject to each scene called SpawnPoint and on scene load set the players transform.position to the position of SpawnPoint. To change music you could just create a second audio source and crossfade with the old one.
You could do all this in the OnLevelWasLoaded() method.
And how exactly do I set the player's transform.position to the position of SpawnPoint? I've edited the original question to include my code.
ins$$anonymous$$d of transform.position(SpawnPoint.transform) do this: transform.position = spawnPoint.transform.position;
On another note variables should always start with a small letter to make it easier to differentiate them from functions and types.
For some reason this still does not load the player into the proper position... It still wants to load at the original position the object was created in....Even if youve deleted this, even with this code in the OnLevelWasLoaded function, the player still seems to be overridden by some kindof internal Unity player preference save.
Your answer
Follow this Question
Related Questions
How to change a model into an other model without clone-ing 1 Answer
problem changing spawnTime variable 1 Answer
How to start/stop/loop different music tracks 0 Answers
Spawning In Photon C# 1 Answer
portal problem 1 Answer