Hi,
I am currently creating a game based off a tutorial and converting it for the iPhone with lot of my own additions. I cant get the scoring system to work.
Basically, Have written this script and added it to the main camera. i want the score to be 1 per kill. is there something wrong with it ?
static var SCORE = 0;
function update()
{
var hit : RaycastHit;
if(hit.collider.gameObject.tag == "Enemy")
{
SCORE++;
GameObject.Find("g_count").guiText.text = "" +SCORE;
}
}
Thanks a lot in advance. Also, is there any place where i can learn more about scripting for the iPhone and perhaps see some examples. I remember of it once being a sticky.
Thanks once again
Firstly, the name of the Update function needs to be spelt with a capital U:-
function Update() {
...
}
(It may seem pedantic, but the language treats “update” and “Update” as two different names because of the upper/lower case.)
Secondly, the RaycastHit variable doesn’t automatically contain the object that was hit. You need to use a raycast to get this information. However, a raycast isn’t necessarily the best way to do this. Can you give any more detail about how your game works (how the enemies are targetted, etc)?
Thank you very much. Basically, its just a top down shooter that i am working on as a practice project. Enemies come in waves from empty gameobjects put throughout the world. To target an enemy the user simply taps towards the direction of the enemy and bullets are fired in that direction and any enemy in the path is shot down. I need a way to track how many kills the user has done. What do i need to fix this script ?
Thanks a lot once again.
There’s nothing wrong with your script as far as incrementing the SCORE. But you need to make sure you are registering hits on your enemies.
How you do that is up to you. You could add a collider to each enemy and call OnCollisionEnter in an attached script to detect for the enemy being killed.
Thanks a lot recluse it worked. But as soon as enemies spawned it counted all the enemies. is there a way to track just the collision on a particular prefab ?
Thanks once again
Thanks recluse and giyomu i have done the exact same thing. However, The problem is that when my enemies spawn they are literally spawning joint together thus colliding. So my script is now counting the bullets fired and colliding as well as all of them spawning. is there a way of me only tracking the bullet prefab ? if yes then how ? thanks once again for all your help.