ending space shooter game

Hi guys I am almost done with space shooter tutorial I just need to end the game so I came up with a code that checks if the player is alive or not and breaks the spawning hazards loop to display game over and press r to restart . here it is :

var hazard : GameObject;
var spawnValues : Vector3;
var hazardCount : int;
var spawnWait : float;
var startWait : float;
var waveWait : float;
static var score : int;

function Start () {
 
   SpawnWaves ();
  
   
} 


function SpawnWaves () {
    yield WaitForSeconds (startWait);
    while (true)
    {
        for ( var i : int= 0; i < hazardCount; i++)
        {
             var spawnPosition : Vector3= new Vector3 (Random.Range (-12,12),0,17);
             var spawnRotation : Quaternion= Quaternion.identity;
            Instantiate (hazard, spawnPosition, spawnRotation);
            yield WaitForSeconds (1);
        }
        yield WaitForSeconds (1); 
        }             
 }   


function OnGUI () {
		GUI.Label (Rect (10, 10, 100, 20), "Score: " + score);
		}
		
		
		
		 if (other.tag "Player" == null) {
        break;
        function OnGUI () {
        GUI.Label (Rect(50,50,50,20), "Game Over");
        }
        function OnGUI () {
        GUI.Label (Rect( 70,70,50,20), "Press R to Try Again");
        }
         if (Input.GetKeyDown (KeyCode.R))
        {
            Application.LoadLevel (Application.loadedLevel);
        }
        }

there are some errors with it I hope that you help me out with it .

what are the errors?

–

First fix your errors, starting on line 39, why in gods name is an if statement outside of a function/method?, why does it open to just break, followed by another function for OnGUI, and another OnGUI function below that. You're presenting a completely mal-formed script and asking for help ending the shooter game you're working on, these are two very different things and your broken implementation shows very little effort to fix your mistakes.

–

It is ok I get it . I need to figure things on my own to become a better programmer thanks .

–

1 Answer

1

Put all OnGUI items in to one function OnGUI(). Don’t attempt to use multiple instances of it in your script.

I need to close this QA.

I suggest you look at other script examples to get a better idea of how to structure your code. Don’t take it personally but answering your question would be like finishing the tutorial / writing your game for you.