Maintaining a variable using a gamecontroller

Yeah, I’m back after only 4-1/2 hours sleep. Beyond the whole forklift parent-child problem, I have an easier one this AM. If someone would volunteer, I’d be grateful.

I have a gamecontroller object at the beginning of the game not being destroyed.

I have three levels … the only difference between them is the time remaining. Easy 180 seconds, medium 120 seconds, hard 75 seconds.

And yet, foolishly, I’m trying to maintain three identical complex scenes.

So, instead of the having the Easy/Medium/Hard options on the splash screen take you to three different scenes I’d simply like to declare the seconds remaining in the game controller and load one scene with the the time remaining display set to the proper seconds depending on which “level” a user selected.

This means keeping my variable (timeRemaining) as you go from the the splash screen to the game screen. Easy enough, but this is the code I have in the DisplayTimer GUIText object …

var secondsLeftEasy = 181.0;
var submitStuff : Transform;

// here's where we display the time remaining
var timershow : GUIText;

private var done : boolean = false;

function Update () {

   // start time
   Time.timeScale = 1;
   
   // if time is less than 0 end scene and go to scoring scene
   // mathf.floor forces it to round to 0
   timershow.text = "Time Remaining = " + Mathf.Floor(secondsLeftEasy);
   secondsLeftEasy -= Time.deltaTime;
       
   if (secondsLeftEasy < 0) {
      timershow.text = "Time's up!\nEnter your name and\nclick Submit Score.";
      Time.timeScale = 0;
      MoveItemsIn();
   }
}


// move the enter name filed and submit score button in from off camera
function MoveItemsIn ()
{
	submitStuff.position = Vector3 (1.022799,-2.656232,-1.859603);
}

See the “var secondsLeftEasy = 181.0;” part? I have three of these scripts, one for each level. I’d rather have a single level with one DisplayTimer item that knows which of the three levesl you chose in an earlier scene.

How do I modify the gamecontroller script and the above script to do this?

Gamecontroller script relevant part to load one of the levels shown as follows …

function LoadEasy () {
	// Load the Easy level
	Application.LoadLevel ("leveleasy");
}

Thanks and good morning everyone!

You don’t even need to mess with the gamecontroller if you don’t want to. In your displayscore script you could something like this:

var secondsLeft; 
var submitStuff : Transform; 

// here's where we display the time remaining 
var timershow : GUIText; 

private var done : boolean = false; 

function Start()
{
   if( Application.loadedLevelName == "leveleasy" )
   {
      secondsLeft = 180;
   }

   if( Application.loadedLevelName == "levelmedium" )
   {
      secondsLeft = 120;
   }

   if( Application.loadedLevelName == "levelhard" )
   {
      secondsLeft = 75;
   }
}

function Update () { 

   // start time 
   Time.timeScale = 1; 
    
   // if time is less than 0 end scene and go to scoring scene 
   // mathf.floor forces it to round to 0 
   timershow.text = "Time Remaining = " + Mathf.Floor(secondsLeft); 
   secondsLeft -= Time.deltaTime; 
        
   if (secondsLeft < 0) { 
      timershow.text = "Time's up!\nEnter your name and\nclick Submit Score."; 
      Time.timeScale = 0; 
      MoveItemsIn(); 
   } 
} 


// move the enter name filed and submit score button in from off camera 
function MoveItemsIn () 
{ 
   submitStuff.position = Vector3 (1.022799,-2.656232,-1.859603); 
}

I think the point is to have one scene…that code depends on having three separate scenes still, with different level names.

What you want to do is use DontDestroyOnLoad, which is what you already have set up, yes? If you look at the project for my Unity Invaders game, you see a stub scene called Setup that contains a manager script. This scene is loaded just once, so you don’t get multiple copies of the manager script. In this case you’d want:

static var secondsLeft : float = 0;
static var instance : GameManager;

function Awake() {
    instance = this;
    DontDestroyOnLoad (this);
}

That’s assuming you call the script GameManager. If you call it something else, change the second line appropriately. Then instead of loading different levels, you do:

function LoadEasy () { 
   // Set up Easy level
   GameManager.secondsLeft = 181;
   Application.LoadLevel ("MainGame"); 
}

etc., for the different loading functions. So you just have one level called “MainGame”, and your timer code would be:

   timershow.text = "Time Remaining = " + Mathf.Floor(GameManager.secondsLeft); 
   GameManager.secondsLeft -= Time.deltaTime;

And get rid of the “var secondsLeftEasy = 181.0;” line, of course.

–Eric

Yeah, I just noticed that :stuck_out_tongue:

Anyways, check Davey’s other thread. He posted his whole project, and I fixed it to use one scene, and all that. But there is still the problem with the Box.

Davey,

Do as Eric says… the key thing is that any variable you want to be able to use across scripts that are declared in the gamecontroller (or startup script) need the static command first.

static var timeleft : float = 180;

The static command will make it like a “global” variable of old… so that any script can access it. So you can set it in the setup with a default value, then in the interface level, set it according to what the user decides to play, then the controller script for the main game uses it to set time.

I’m now back. Smaller than usual family reunion with maybe only 100 Joneses there (the Janik- part of ym last nae came from marrying my wife, we both took each other’s name). Naturally, there was a second David Jones there again, and three Howard Joneses (one may dad, one his dad).

This is what I was looking for and haven’t yet jumped into my other thread. A global variable. I have a splash screen that contain my gamecontroller and on click goes to the splash screen so as to not make multiple copies. This is perfect Eric, thanks. I’ll have a go at whacking it into place right now. Then delve into my other issue.