i wrote a script for counting the score and saved as score but i am not sure where i have to place and how to display that score. I whole programe I have three object. 1. Player, 2. Enemy, 3. Bullet. Please help me. Please below the code for counting and tell its correct or not.
static var score:int=0;
function OnTriggerEnter(otherObject:Collider)
{
if(otherObject.gameObject.tag==(“enemy”))
{
gameObject.Destroy(otherObject.gameObject);
score ++;
the static var score goes in the player script, I will call it Player.js. The score belongs to him.
The second part I would attach it to the enemy and modify a little. I know it is logical but it makes thing easier to me
var health = 100; // healt of your enemy
function OnTriggerEnter(otherObject:Collider) {
if(otherObject.gameObject.tag==("bullet")) {
Player.score +=1; // decrease score
Destroy(other.gameObject); // destroy otherObject, the bullet
health -= 10; // decrease health
if (health<=0) // check health
Destroy(gameObject); // kill enemy
}
}
If you wanted to attach it to the bullet, it is possible BUT you would have to GetComponent() the script of the enemy to access the health. Do not make the health of the enemy static!!! Look on the forum about static.
Add a GuiText object to your scene GameObject->CreateOther->GuiText
Place it and attach a new script to it with
function Update(){
guiText.text = "Score: "+Player.score;
}
You can also use OnGui but I find GuiText more flexible and easier to use.