"I've made a test game, need help with my health script" (434550)

Hi everybody :slight_smile:
I’m gonna make a indie game, i’ve made a test game for testing its scripts,
unfortunately i have a problem with my health script,
whenever i die, and i try to load it again its health system remains 0 so it says game over again and again
here is the game with web player format 18 mb click here for download
And i think the problem is with my turret script do damage part
BTW i did this scripts with help of my video tutorial from vtc.

this is my turret script: with javascript:

var target : Transform; 
var range = 10.0; 
var LeftFlame :  GameObject;
var RightFlame : GameObject;
static var mode = "idel";

function Awake()
{
	if(!target)
	{
		target = GameObject.FindWithTag("Player").transform;
		LeftFlame.renderer.enabled = false;
		RightFlame.renderer.enabled = false;

	}
}
function Update ()  
{
	if(target  CanAttackTarget())
	{
		//transform.LookAt(target);
		var targetRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
		transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 1.2);
		var forward = transform.forward;
		var targetDir = target.position - transform.position;
		var angle = Vector3.Angle(targetDir, forward);
		if(angle <10.0)
		{
		
		[COLOR="red"]DoDamage();[/COLOR]
		
		}
		else
		{
			
		}
	}
}
[COLOR="red"]var damageTimer = 0.0;
function DoDamage()
{
	if(damageTimer==0.0)
	{
		damageTimer = Time.time;
	}
	if((damageTimer+0.05) > Time.time)
	{
		return;
	}
	else
	{
		Player.HEALTH -= 1;
		print(Player.HEALTH);
		damageTimer = Time.time;
	}
}

function CanAttackTarget() 
{
	//Check if the target is close enough
	if(Vector3.Distance(transform.position, target.position) > range)
	{	Disengage();
		return false;
	}
	
	var hit : RaycastHit;
	//Check if there's collision in between turret  target
	if(Physics.Linecast(transform.position, target.position, hit))
	{
		if(hit.collider.gameObject.tag !="Player")	
		{
			Disengage();
			return false;
		}
		else
		{
			Attack();
			return true;
		}
		
	}
	
	return true;
}

function Attack()
{
	if(mode != "attack")
	{
		InvokeRepeating("FalconAnimate", 2, 0.05);
		mode = "attack";
	}

}


function Disengage()
{
	if(mode !="idel")
	{
		CancelInvoke();
		mode = "idel";
		LeftFlame.renderer.enabled = false;
		RightFlame.renderer.enabled = false;
	
	}


}

[/COLOR]
InvokeRepeating("FalconAnimate", 0, 0.05);

function FalconAnimate()
{
	if(LeftFlame  RightFlame)
	{
		if(LeftFlame.renderer.enabled)
		{
			LeftFlame.renderer.enabled = false;
			RightFlame.renderer.enabled = true;
			
		}
		else
		{
			LeftFlame.renderer.enabled = true;
			RightFlame.renderer.enabled = false;
		}
	
	}
	else
	{
		print("Effect on Turret not set!");
	}
}

and this is my player script:

var h00 : Texture2D;
var h10 : Texture2D;
var h20 : Texture2D;
var h30 : Texture2D;
var h40 : Texture2D;
var h50 : Texture2D;
var h60 : Texture2D;
var h70 : Texture2D;
var h80 : Texture2D;


static var HEALTH = 80;

function Update()
{
	var g_Health = gameObject.Find("g_Health");  
	
	if(HEALTH > 70)
	{
		g_Health.guiTexture.texture = h80;
		return;
	}
	else if (HEALTH > 60)
	{
		g_Health.guiTexture.texture = h70;
		return;
	}
	else if (HEALTH > 50)
	{
		g_Health.guiTexture.texture = h60;
		return;
	}
	else if (HEALTH > 40)
	{
		g_Health.guiTexture.texture = h50;
		return;
	}
	else if (HEALTH > 30)
	{
		g_Health.guiTexture.texture = h40;
		return;
	}
	else if (HEALTH > 20)
	{
		g_Health.guiTexture.texture = h30;
		return;
	}
	else if (HEALTH > 10)
	{
		g_Health.guiTexture.texture = h20;
		return;
	}
	else if (HEALTH > 5)
	{
		g_Health.guiTexture.texture = h10;
		return;
	}

	 else if (HEALTH <= 0)
	
	{
		g_Health.guiTexture.texture = h00;
		Application.LoadLevel(2);
		return;
	}
		
	
}

/*InvokeRepeating("subtract", 2, 0.5); 
 
function subtract()
{
HEALTH -= 1;
print("health is now: "+HEALTH);

}*/

Thanks :smile:

So what exactly do you need, a script to reset the health and the terrain collision?

I’m stuck at that health part, which when i die in the game and try to play again my health system remains 0 and it says game over…

in your player script it doesn’t look like you reset the health amount?

how should i correct it?

What is LoadLevel(2) supposed to do?

just set the HEALTH variable to whatever full is before it resets? that might solve it

it brings another scene which say game over

should i use another if? switch while? I’m new at Java i only now little about C++

does the character go with the scene or does the scene have a different or no character?

The two other scenes don’t have characters only camera

So are you saying that the first time you run this, it works correctly, but if you close it, then start it playing again, it doesn’t work correctly? (If this is not the case please clarify)

No it works for the first time but when the A.I kill me and i try to play it again it goes to game but my health still remains 0 so quickly it goes to scene 2 which is the game over part…my problem is i need to write a script to tell it when you die put the health system on 100%…

Where were you? come back please :smile:

did you put HEALTH = 100?

put that right before the Application.LoadLevel(2);

Yea it didn’t help

so what exactly is the second scene that it’s loading?

well you can download it, it’s jut 18mb, it has 3 scenes, 1 for the main menu which you can click play button there and another scene is for game over part and the 3rd for the real game they don’t have problem imo

Okay guys i need to go and thanks for helping, still i didn’t get my answer though, for the first time it works alright i start the game, and go to the A.I robot and die, and it shows me the gameover scene, but after i try to play it again it don’t work actually it works but since the health system remains 0 it quickly brings the game over scene, so i need help with a script which reset the health % to 80% so that i can play it again, thanks.