- Home /
Problem with a score script
Hi,
I'm creating a very basic 2D platformer. I have already the basic move functions working. But now i'm trying to make a score script. The idea is that if you hit a red square that you get 1 point. I added a box collider to it that is triggered and i already have the +1 point working, the only problem now is that when i hit the red square, every red square in the scene dissapaers. Every red square is tagged 'score'
This is a part of my move around script:
static var score : int = 0; static var AmtScoreHits : int = 0;
function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "score") { score = score+1; AmtScoreHits = AmtScoreHits +1; } }
And this is the script atached to the red square, the score object:
function Update() { if(MoveAround.AmtScoreHits == 1) { Destroy(gameObject);
} }
And sorry if they're a lot of spelling mistakes but i come from Belgium ;)
Answer by xCRKx TyPHooN · Apr 26, 2011 at 07:16 PM
Try this...
if(hit.gameObject.tag == "score")
{
score++;
AmtScoreHits++;
Destroy(hit.gameObject);
}
This will destroy the gameObject that collides immediately when you pick it up. If you don't want to destroy it immediately, it looks like you are doing everything right EXCEPT this line...
if(MoveAround.AmtScoreHits == 1)
I'm guessing MoveAround is a script? The problem here is that the variable AmtScoreHits in MoveAround is 1 for everything, so every red square is getting that same script and triggering the destroy function.
static var score : int = 0; static var AmtScoreHits : int = 0;
function OnTriggerEnter(hit : Collider){ if(hit.gameObject.tag == "score"){ score++; AmtScoreHits++; Destroy(hit.gameObject); } }
that should work now I have edited it. You only need one script and this is a replacement to the "part of my move script" script that goes on your character or whatever is hitting the scoring collider
hope that helps
Scribe
I didn't triy you're solution because the answer before you worked already thanks anyway :)
both are answers were the same except he was 20seconds faster :)
Ok thank you very much, this was just what i was looking for ;D
Your answer
Follow this Question
Related Questions
Adds too many points to the score? 0 Answers
how do i count leftover enemies in screen? 0 Answers
Scoring System 2 Answers
How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer