laisch
1
How can i add a script to my project wich is executet in every scene the whole time the game is active.
As an example I want to pop up some ads after every x minutes, don’t matter wich level is currently played.
One previus solution for me was to add a script to every scene, but there must be a easier way to do this, because Ihave now about over 50 scenes.
can I add an empty gameobject to my first scene (splash screen) with the script on it, and tell it somehow to stay alive the whole game?
Thx
laisch
2
Thx to savlon, that was what I was searching for.
For other people who are searching for the same solution, I post some of my code here:
public class AdManager : MonoBehaviour {
public float TimeToShowAd = 600f; // in Seconds
private float TimeSinceStart = 0;
private static AdManager instance = null;
// Handling to stay alive forever
void Awake(){
Advertisement.Initialize (gameID, true);
if(instance == null){ // initialize only one instance.
instance = this;
GameObject.DontDestroyOnLoad(this.gameObject);
} else { // Destroy unused instances.
GameObject.Destroy(this.gameObject);
}
}
void Update () {
TimeSinceStart = TimeSinceStart + Time.deltaTime;
if (TimeSinceStart > TimeToShowAd) {
TimeSinceStart = 0;
ShowAd();
}
}
}