Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

AudioSource in Update

$
0
0
Hey guys, I have different background music clips that should play even if I restart the game. For that purpose I have created a Script attached on a gameObject that only plays the music. private var player : GameObject; var mg : AudioClip; var def : AudioClip; function Awake(){ DontDestroyOnLoad(this.gameObject); if (FindObjectsOfType(GetType()).Length > 1){ Destroy(gameObject); //Avoid duplicate on scene restart } } function Start () { player = GameObject.Find("Player"); } function Update () { if(PlayerPrefs.GetInt("TimesPlayed") >= PlayerPrefs.GetInt("OverallScore")){ GetComponent.().clip = mg; GetComponent.().Play(); } if(player.transform.GetChild(0).gameObject.name == "CubeDefault"){ GetComponent.().clip = def; GetComponent.().Play(); } } The problem is that the specified clip is called every frame since it is in the update function, but it needs to be in this function, because - at Runtime the music can change, - the script will never be destroyed and so I can not put this code to the start/awake function as they read the content only once. The solution would be some kind of "if" that change their values to call `GetComponent.().Play();` only once, but how can I do this? Thanks

Viewing all articles
Browse latest Browse all 416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>