- Home /
object destroy and scoring points
i've looked through questions already but i cant seems to find a specific answer/script to my problem. im making a ball rolling game which destroys objects in turn giving you a score. i have 2 scripts at the min:
static var score : float = 0;
function Update () { guiText.text = "Score : " + score;
}
this shows "score:" on the screen and my other script is:
function OnCollisionEnter ( collision : Collision){
if (collision.gameObject.tag == "DestructibleObject")
KeepScore.score++;
}
which is attatched to my player (rolling ball) and updates the "score:" by 1 everytime i hit an object named "DestructibleObject". the problem is that the score keeps increasing, im wanting to limit the score to 1 for each object. it would be even better if anyone can tell me how to destroy the object at the same time. i added the line:
Destroy (collision.gameObject);
so the script looks like:
function OnCollisionEnter ( collision : Collision){
if (collision.gameObject.tag == "DestructibleObject")
KeepScore.score++;
Destroy (collision.gameObject);
}
but when i load the level the terrain is getting destroyed (i think when my ball thouches it to start with). if i remove the keepScore.score++; line i can go round the level fine (it doesn't get destroyed) and when i hit an object it is destroyed. when the 2 lines of script are together it just doesn't want to work : /
any help is highly appreciated
thanks.
Answer by GesterX · May 01, 2011 at 07:50 PM
I believe the problem is with your if statement syntax. It's always best to surround the code after the if with curly braces. So in your code:
var oneTime : boolean = false;
function OnCollisionEnter ( collision : Collision){
if(!oneTime){
if (collision.gameObject.tag == "DestructibleObject"){
KeepScore.score++;
oneTime = true;
}
}
}
Everything in the curly brackets gets called if the if-statement is satisfied
omg! lol that worked perfectly i feel like such a muppet now. im still new to the scripting side of things, i wont be making that mistake again :D
thanks a bunch.
i'm still curious though, if the object isn't getting destroyed what script etc will i have to use to have a maximum of 1 point come from an object (ridgidbody) that still stays there in the scene?
Could you reword that question? I didn't quite understand. Don't forget to mark this answer as the correct one and vote it up :)
sorry if it didn't make any sense. right, ins$$anonymous$$d of the object getting destroyed when i touch it i'de like to know how to keep the object in the scene while reacting with physics (rigidbody) and recieve +1 score only. because at the $$anonymous$$ i can have the object in the scene but when im rolling into it the score will keep rising. basically i want to limit the score to +1 only.
thanks and hope that makes a bit more sense.
I editted the code in the answer. Basically you have a variable called oneTime set to false. When you collide you check if oneTime is false (!oneTime means NOT oneTime). If it's false then the next part of the code is run. At the end of the code we set oneTime to true. This means the next time you collide oneTime will be true and the code after the if statement won't be run.
this works fine but not how i'm wanting it to, it might of been me not explaining it right. since i have multiple objects in my scene tagged 'DestructibleObject' the score rises by 1 when i hit the first object but every other object i hit after that wont give out score. i realised what i should of said is to limit score given out by 1 from each individual object. what i might have to look into now is something like if the object has moved from original position then dont give any more score out.
thanks anyway, i appreciate all your help. il get my head into looking for different solutions :)