Problem with Awake
I have a Canvas with DontDestroyOnLoad attached to it. I have only two things on that canvas, MoneyText and PointsText. When i call Application.loadLevel points should be restarted, but they aren't.
This is the script attached the PointsText:
public float points;
public float pointIncreaser;
private Text pointText;
void Awake()
{
pointText = GameObject.Find ("/Canvas/PointsText").GetComponent<Text> ();
Debug.Log ("asd");
points = 0;
SetPointsText ();
pointIncreaser = 1;
}
void Update ()
{
points += pointIncreaser;
SetPointsText ();
}
void SetPointsText()
{
pointText.text = "" + points.ToString ();
}
when i call application.loadLevel float points remains as it was before loadlevel was called, and pointIncreaser remains 0.
Solution?
Thanks :)
Answer by Teravisor · Jan 27, 2016 at 04:13 PM
Awake() is not called when loading level. Use OnLevelWasLoaded instead.
And in general, see http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html for a list and descriptions of them all.
Answer by mahnur · Apr 16, 2016 at 09:45 PM
using UnityEngine; using System.Collections;
public class onClick : MonoBehaviour { public GameObject music; private static GameObject instance; public AudioSource audio; void Awake() { music = GameObject.FindGameObjectWithTag ("Music"); } public void Options(string optionMenu){ DontDestroyOnLoad(music); instance = music; Application.LoadLevel (optionMenu);
}
public void Back(string mainMenu){
Application.LoadLevel (mainMenu);
if (music == null)
DontDestroyOnLoad (music);
else
Destroy (music);
}
public void Highscores(string highscore){
DontDestroyOnLoad (music);
Application.LoadLevel (highscore);
}
public void VolumeControl()
{
if (music.activeSelf)
music.SetActive (false);
else
music.SetActive (true);
}
}
main menu has audio source...everything is working fine but when i move back to my main menu new audio source is created n previous is destroyed...although according to singelton pattern new shouldnt be created...plz help... i have tried singelton code in awake() n onlevelwasloaded() but all in vein :( n atlast here i have it in my back method