- Home /
How can I make triggering multiple triggers give more points?
I have a score system like this:
Everytime my ball enters my trigger, I get 100 points. But what if I want to get 300 points if my ball enters two triggers, or if my ball enters three triggers I get 500 points?
This is my current code, which doesn't account for multiple triggers:
static var myScore : int = 0;
function OnTriggerEnter() { myScore += 100; Debug.Log(myScore); }
function Update () { if (myScore >= 1000) { Application.LoadLevel (1); } }
I hope you understand this, and maybe somebody could please help me?
Answer by The_r0nin · Dec 16, 2010 at 07:36 PM
Assuming that your triggers are stationary and do not get destroyed when hit: Add a class variable (call it "timeOfHit" or something) for the time and a class variable for the last trigger hit (you'll need to tag your triggers for a function... more on this later). Then when you hit a trigger record the Time (timeOfHit = Time.time;) and the tag (you'll need to have the trigger return a number or string when the function is called). On the next collision, check to see if the time is close enough to the last time (whatever you want your threshold to be) and you are hitting a different trigger. If so, give multiple points...
On the player:
var timeOfHit:float =0.0; var lastTriggerHit: int = 0; var playerScore: int = 0;
function HitScore(triggerNumber:int){ var thisHitTime:float = Time.time;
if (triggerNumber != lastTriggerHit){
lastTriggerHit = triggerNumber;
if ((thisHitTime-timeOfHit) > 2.0){
playerScore += 100;
}
else{
playerScore +=300;
}
timeOfHit = thisHitTime;
}
}
On the Trigger:
var myNumber:int = 1; // different value for each trigger
OnTriggerEnter(other:Collider){ other.gameObject.BroadcastMessage("HitScore",myNumber); }
This should work (I haven't tested it)... the important thing is to record which trigger was hit and how long ago (if you want to make the player hit nearby triggers close together for an increased score).
Thank you for your help but something dont work properly.I get the message score player.js(16,21): BCE0005: $$anonymous$$ identifier: 'thisTime'.
The variable should be thisHitTime in both places. I've edited the script above.
Don't forget to click the checkmark to the left of the answer (so everyone will know the problem has been solved). Glad I could help.
ok one last question maybe you could help me again....the score of the player is shown but my player is a ball (prefab) and the score will be shown correctly untill my prefab is destroyed.then the counter starts from 0. Is there a way to sum the points every prefab got in his lifetime untill he gets destroyed??
Your answer
![](https://koobas.hobune.stream/wayback/20220613091217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
After I initially add score it keeps adding. 2 Answers
OnTriggerEnter to load new level 1 Answer
Point system help! 2 Answers
Point system, something wrong 1 Answer