- Home /
Health Script
Hey, I am kinda a beginner at scripting, closer to intermediate. I am getting an error and yes, I know that the script is called Bullet and all that stuff. Here is the error Assets/Scripts/Guns and Damaging/Gun.js(38,13): BCE0067: There is already a local variable with the name 'bullet'. And Here is the code #pragma strict
var Health : int = 100;
var bullet : GameObject;
var damage : int;
function Update ()
{
if(Health < 0)
{
Die();
}
}
function OnGUI ()
{
GUI.Box(Rect(10,Screen.height - 150, 200, 145), "");
GUI.Label(Rect(20,Screen.height - 140, 180, 125), Health + " / " + "100");
}
function OnTriggerEnter ( hit : Collider )
{
if(hit.gameObject.tag == "RedBullet")
{
bullet = hit.gameObject;
TakeDamage();
}
if(hit.gameObject.tag == "RedBullet")
{
bullet = hit.gameObject;
TakeDamage();
}
}
function TakeDamage()
{
var BulletScript : Bullet = bullet.GetComponent("Bullet");
damage = BulletScript.Damage;
Health -= damage;
}
function Die()
{
Debug.Log("I'm death");
Network.Destroy(gameObject);
var spawnObject : GameObject = GameObject.Find("SpawnManager");
var spawnScript : SpawnManager = spawnObject.GetComponent("SpawnManager");
spawnScript.Dead = true;
}
When you double-click the error, what line of code is marked?
the one with BulletScript : Bullet = bullet.GetCompnent("Bullet");
Answer by Chronos-L · Mar 03, 2013 at 04:19 AM
Because you do not format your code properly ( the #pragma strict
is not in the code segment ), I can't really tell which line is line 38.
If anything in your script can be wrong, that something is most probably the TakeDamage()
.
The line is var BulletScript : Bullet = bullet.GetComponent("Bullet");
I have copied your code and ran it. There is no error at all, just 1 warning.
$$anonymous$$y $$anonymous$$ake-shift Bullet Script
var damage : float = 5;
function Update ()
{
transform.Translate( Vector3.forward * 3 * Time.deltaTime );
}
The Health Script
pragma strict
var health : int = 100;
var bullet : GameObject;
var damage : int;
function Update ()
{
if(health < 0)
{
Die();
}
}
function OnGUI ()
{
GUI.Box(Rect(10,Screen.height - 150, 200, 145), "");
GUI.Label(Rect(20,Screen.height - 140, 180, 125), health + " / " + "100");
}
function OnTriggerEnter ( hit : Collider )
{
Debug.Log("trigger");
if(hit.gameObject.tag == "RedBullet")
{
bullet = hit.gameObject;
TakeDamage();
}
if(hit.gameObject.tag == "RedBullet")
{
bullet = hit.gameObject;
TakeDamage();
}
}
function TakeDamage()
{
var bulletScript : Bullet = bullet.GetComponent("Bullet") as Bullet;
damage = bulletScript.damage;
health -= damage;
}
function Die()
{
Debug.Log("I'm death");
Network.Destroy(gameObject);
/*var spawnObject : GameObject = GameObject.Find("Spawn$$anonymous$$anager");
var spawnScript : Spawn$$anonymous$$anager = spawnObject.GetComponent("Spawn$$anonymous$$anager");
spawnScript.Dead = true;*/
}