Hi Sammual, I would attach something, but I don’t have any server space (thats on my list of todo’s).
I can tell you in code though basically how to get this started.
The current project I am working on, my very first scene is designed to handle the opening logos, but I also have my most valuable object in that scene and its GameManager.
This object has a script that holds references to everything in my game, including the main menu. The reason I did it this way is simply because I find it easier to reference one script throughout the game when I want to influence game decisions, such as “all my lives are gone”, or “the user has decided to go back to the main menu”, and many more things, those are just 2 examples.
So here’s a small take of GameManager , keep in mind, I program in javascript not c#:
// Start.scene
// Desc: Empty game object that has the 2 following scripts attached
// GameManager.js
// DontDestroyObject.js
// GameManager.js
// game setup values
var menuMusic : AudioClip;
var gameMusic : Audioclip;
var maxScenes : int = 26;
var highScore : int = 1000;
var gameScore : int = 0;
var gameLevel : int = 1;
// gui setup values
var coverTexture : Texture2D;
var logoTextures : Texture2D;
var menuTexture : Texture2D;
var coverFade : GameObject;
var pauseGame : GameObject;
var life1 : GameObject;
var life2 : GameObject;
var life3 : GameObject;
var levelText : GameObject;
var scoreText : GameObject;
var timerText : GameObject;
var gameWonText : GameObject;
var GWDelay : float = 3.0; // Game Won text delay time
var levelUpText : GameObject;
var LUDelay : float = 3.0; // Level Up text delay time
var readyGameText : GameObject;
var RGTDelay : float = 3.0; // Ready Game text delay time
var gameOverText : GameObject;
var GODelay : float = 3.0; // Game Over text delay
// game control values
private var gameReady : boolean = false; // should we show the "Ready" text
private var gameWait : boolean = true; // run condition of all object loops, suspend them for other game states to process
private var gameWon : boolean = false; //diddo
private var gamePause : int = -1; // are we at a paused state (-1 is neither paused or unpaused, 0 is unpaused, 1 is paused)
private var gameQuit : boolean = false; // should we quit the game?
private var gameMenu : boolean = true; // should we go back to menu?
private var gameInst : boolean = false; // should we go back to instructions?
private var gameStats : boolean = false; // should we display the game craft stats?
private var gameRestart : boolean = false; // should we restart the game
private var levelCleared : boolean = false; // did we clear the level?
private var displayStats : boolean = false; // are we shoing the stats screen?
I’ll stop here and explain some, I know that not all games will be like this, but you can see in my GameManger script, I am making references to alot of game objects, and variables. With the exception of a Texture and some control variables, all of these don’t have anything to do with the Start scene, they are mainly used in the other game scenes, but again my reason for this is to have one place to control the game from, it just makes it easier to manage and keep up with how the game should flow.
Now the neat part is adding the DontDestroyObject script which simply goes like:
function Awake() {
DontDestroyOnLoad(this);
}
Easy enough, and now when I load my Menu.scene, my GameManager object will be accessible in that scene, and when I load my Game scene, my GameManager object is accessible from there too.
So since my Start.scene holds all my menu/game hud stuff(textures, texts, etc.) I also to put the DontDestroyObject on those game objects and carry them over to the other scenes where I can access them to update them however I need to.
Took me a while to get my head wrapped around it all, but once you understand how you can share objects between scenes like this, it really makes a difference in your programming, you can be really creative.
Finally, one last note on GameManager, in any of my other game scripts, when I need to update say the “gameLevel” global that is defined in GameManager, in my other script, I start by getting a reference to GameManager:
private var manager : GameManager;
function Start() {
manager = gameObject.FindObjectOfType(GameManager);
}
Now you have full control of GameManager in that script and can update values, change game control state booleans, whatever.
For example, let’s give an extra life, so we need to update gameLives in GameManager
//... in your other game scenes function
manager.GetComponent(GameManager).gameLives += 1;
//....
Then you can take it another step and update the GUIText that displays your lives(remember I built a GUIText object back in the Start scene, and applied DontDestroyObject to it).
This part explains “static” on my GUIText, here is the code for it:
static var levelText : GUIText;
function Awake() {
levelText = gameObject.GetComponent(GUIText);
}
static function ShowLevel(level : int) {
levelText.text = "Level : " + level.ToString();
}
Keep in mind a static function requires static variables, also remember you have to have in instance of this in order to call the function. So since this has a static variable and function, and… you have access to it because even though this object was built in the Start.scene, but you added DontDestroyObject to it, which carried it over to the Game.scene, you have full control over it and once you have increased your “gameLevel” in GameManager, you can update your text level display in the game by simply calling:
... in your other game scenes function
// you've updated your gameLevel, now update your display
var newLevel = manager.GetComponent(GameManager).gameLives;
GUITextLevel.ShowLevel(newLevel);
// ....
I hope that makes sense, as you can see your referencing GUITextLevel which is the script name of the GUIText object that shows your current level on the screen. Since it’s all static, you don’t have to access it through FindObjectOfType();
Thats all there is to it! I’m no expert though, so if any other programmers would like to comment on my process, or inform me of better ways of doing what I’m doing, I’m always open for suggestions.
Hope that helps you understand better.
-Raiden