this is my full scripts:
1,the PlayerControl.js:(where i called my Lose() function).
var bottomThruster_R : ParticleSystem;
var bottomThruster_L : ParticleSystem;
var topThruster : ParticleSystem;
var leftThruster : ParticleSystem;
var rightThruster : ParticleSystem;
var explosion : ParticleSystem;
var hardness : int = 2;
var GUI : InGameGUI;
function Start () {
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function FixedUpdate ()
{
if(Input.GetAxis("Horizontal") > 0)//checking for right arrow key
{
leftThruster.enableEmission = true;
rigidbody.AddForce(10,0,0);
}
if(Input.GetAxis("Horizontal") < 0)//checking for left arrow key
{
rightThruster.enableEmission = true;
rigidbody.AddForce(-10,0,0);
}
if(Input.GetAxis("Horizontal") == 0)//checking if no horizontal keys down
{
leftThruster.enableEmission = false;
rightThruster.enableEmission = false;
}
if(Input.GetAxis("Vertical") > 0)//checking for up arrow key
{
bottomThruster_L.enableEmission = true;
bottomThruster_R.enableEmission = true;
rigidbody.AddForce(0,10,0);
}
if(Input.GetAxis("Vertical") < 0)//checking for Down arrow key
{
topThruster.enableEmission = true;
rigidbody.AddForce(0,-10,0);
}
if(Input.GetAxis("Vertical") == 0)//checking if no vertical keys down
{
bottomThruster_L.enableEmission = false;
bottomThruster_R.enableEmission = false;
topThruster.enableEmission = false;
}
}
function OnCollisionEnter (col : Collision) {
if(col.relativeVelocity.magnitude >= hardness){
Explode();
}
else if(col.gameObject.tag == "Platform"){
Debug.Log("Platform hit!");
}
}
function Explode(){
GUI.Lose();
Instantiate(explosion,transform.position,Quaternion.identity);
Destroy(gameObject);
}
2,this is the InGameGUI script,where i have my Lose() function defined.
var guiMode : String = "InGame";
var numActivated : int;
var totalLZ : int;
function start (){
}
function Update(){
if(Input.GetKeyDown("escape")){
Time.timeScale = 0;
guiMode = "Paused";
}
}
function OnGUI(){
if(guiMode == "Paused"){
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2-20,150,30),"Resume Game")){
Time.timeScale = 1;
guiMode = "InGame";
print("starting new game...");
}
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2+20,150,30),"Quit to Main Menu")){
Time.timeScale = 1;
print("continue game...");
Application.LoadLevel(0);
}
}
if(guiMode == "Win"){
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2-20,150,30),"Next Level")){
Time.timeScale = 1;
guiMode = "InGame";
Application.LoadLevel(Application.loadedLevel + 1);
print("Next level");
}
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2+20,150,30),"Quit to Main Menu")){
Time.timeScale = 1;
print("quiting...");
Application.LoadLevel(0);
}
}
if(guiMode == "Lose"){
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2-20,150,30),"Retry Level")){
Time.timeScale = 1;
guiMode = "InGame";
Application.LoadLevel(Application.loadedLevel);
print("Retry level");
}
if(GUI.Button(Rect(Screen.width/2-75,Screen.height/2+20,150,30),"Quit to Main Menu")){
Time.timeScale = 1;
print("quiting...");
Application.LoadLevel(0);
}
}
}
function LZactivated(){
numActivated++;
if(numActivated == totalLZ){
Win();
}
}
function Win(){
guiMode = "Win";
Time.timeScale = 0;
PlayerPrefs.SetInt("playerLevel",Application.loadedLevel+1);
}
function Lose(){
Wait();
}
function Wait(){
yield WaitForSeconds(1.5);
Time.timeScale = 0;
guiMode = "Lose";
}