- Home /
Trouble with background music and listeners.
First off, I am a beginner; I've only started using Unity 2 days ago so I'm not familiar with a lot of Unity has to over yet.
Anyway, I'm trying to make a game where: 1. The audio from the first scene follows my player through each scene and 2. on one scene there is a song that progressively gets louder but it isn't the same scene the first song is started in.
What I have right now is a Camera that has the first Audio Source and the camera doesn't get destroyed upon changing scenes. Now, I know this is where something weird is happening but I can't figure out what. I have a new camera on each scene that follows the player because I can't get the first camera to track the newly spawned player. This camera has no audio listener. The problem with keeping the same player is getting it to spawn on the right coordinates in the next scene. Because of this, it seems the one camera stays in the same spot and doesn't follow the player to the area with the new music.
My ideal situation would be for the camera and player to go to every scene, in the spot I want them to, and then have the listener on the camera pick up all the sound it can, which it should if it follows the player. Can someone please help me figure out what to do to get it to work the right way?
Perhaps you could try teleporting your character rather than switching scenes? Could you elaborate on the question? And is your audio source attached to your camera, as a component?
So you're saying I should maybe just put all the scenes into one? Yeah, I'm sorry my question was so weird; it's just been a lot of stuff going back and forth and messing with things. I don't think my audio source is a component; I have it under the $$anonymous$$ain Camera in the hierarchy. $$anonymous$$y question is how to properly get a Camera and/or AudioSource to stay active between scenes. If there's a way to get the camera to work like that, there's going to be more I need help with.
Answer by ScienceIsAwesome · Jul 25, 2015 at 03:01 AM
Any GameObject and its children can be preserved between scenes by using:
DontDestroyOnLoad(this);
Your character can at any time have its position changed to anything, you could simply create Spawn Location gameobjects, place one in each scene and upon load tell the character go-to-there with something like:
transform.position = GameObject.Find("SpawnLocation").transform.position;
The camera can be a child of the character or a flying camera trying to keep a certain distance and orientation relative to the character, in that case it would have something like Transform target in a script and would move according to the target with Update or FixedUpdate, depending on whether or not the camera is a rigibody. If it is a dynamic camera that gets left behind, it could simply have code that says "as soon as I'm too far away, I'll appear right behind that bastard!". That would also handle the camera getting stuck.
Ahhh okay I see. I kind of had this working but it was weird but I've been able to fix it so it works the way I want. Thank you! Now, another related question: how do I keep text between two scenes? The way my primitive game works is that once you collect all the collectables, you a prompt to hit space to proceed to the next scene. However, I have the number of collectables collected as a public variable that should be different per level. I have this and a score connected to my player. When I get to the new scene, the score and count number turn into default text and I am unable to proceed to the next scene as the conditions to go to the next scene are on the player. How should I change this so the count is updated correctly and the score doesn't disappear?
I recommend keeping globals in a $$anonymous$$onoBehavior manager, that way the manager can use DontDestroyOnLoad(this); as well as subroutines and other useful things. So for example:
using UnityEngine;
public class Test : $$anonymous$$onoBehaviour
{
public static int globalInt;
public static string globalString;
public static float globalScore;
void Start ()
{
DontDestroyOnLoad(this);
}
}
This has to be placed on a gameobject, it can be an empty one, and it will survive any loading that takes place. You can use the variables anywhere with:
Test.globalScore = 5.43f;
float x = Test.globalScore;
They truly are global, they are persistent, notice the 'static' keyword which makes them unique so you can simply write the name of the class followed by the name of the variable.
Yeah that's basically what I have but it's not working :/ I made a new question for it here: http://answers.unity3d.com/questions/1015411/missingreferenceexception-the-object-of-type-text.html