- Home /
How to enter trigger a certain amount of times before executing code?
I making a score system and I'm trying to make it so that every time the player hits the ball the score goes up. The max score is 4. I'm doing this by deactivating the number and activating the next number. The problem is that when the player hits the ball the score automatically goes to 4. I'm trying to make it so that the score equals the amount of times you hit the ball. Here is my code void OnTriggerEnter(Collider other) { if (other.gameObject.name == "hit") { v3_Forcex = t_Camera.forward; v3_Forcey = t_Camera.up;
GetComponent<Rigidbody>().AddForce(v3_Forcex * f_Multiplierx, ForceMode.Force);
GetComponent<Rigidbody>().AddForce(v3_Forcey * f_Multipliery, ForceMode.Force);
GetComponent<Rigidbody>().useGravity = true;
if(defensescoring.activeInHierarchy)
{
playerscore0.SetActive(false);
playerscore1.SetActive(true);
}
if (defensescoring&playerscore1.activeInHierarchy)
{
playerscore1.SetActive(false);
playerscore2.SetActive(true);
}
if (defensescoring&playerscore2.activeInHierarchy)
{
playerscore2.SetActive(false);
playerscore3.SetActive(true);
}
if (defensescoring&playerscore3.activeInHierarchy)
{
playerscore3.SetActive(false);
playerscore4.SetActive(true);
}
}
}
Hmm lets try this out see how she goes. think it may be as simple as this:
Start Time = 11:16am
//Lets make it adjustable
public int $$anonymous$$axHits=4;
public int Score;
public static PlayerName;
private Text scoreText;
private static List<Collider> collisions=new List<Collider>();
void OnEnable(){
collisions=new List<Collider>();
scoreText=GameObject.FindWithTag("ScoreTxt").GetComponent<Text>();
// Commented out in-case 'Player.name' Doesn't exist in your project
//PlayerName=Player.name;
}
void Update()
{
*Edit: Forgot a line.*
scoreText.text=Score.ToString();
//If we are less then the max hits do something
if(Equals(collisions.Count>=$$anonymous$$axHits)){
//Do Something
foreach(Collider col in collisions){
Score++;
}
}
}
OnDisable(){
ScoreContainer.Scores.Add(Score);
ScoreContainer.Names.Add(PlayerName);
}
OnTriggerEnter(Collider col)
{
if(Equals(collisions.Count,$$anonymous$$axHits)){
return;
}
collisions.Add(col);
}
public static class ScoreContainer{
public static List<string>Names=new List<string>();
public static List<int>Scores=new List<int>();
}
Finish Time = 11.45 A$$anonymous$$
Time to Write = 29 $$anonymous$$ Time To Edit = 5 $$anonymous$$, Total = 34 $$anonymous$$
Notes Score will always = 4 and never above. you will need a score modifier of some sort to change that.
Answer by UltraGearGames · Feb 16, 2019 at 08:32 PM
You could make a set of bools and activate them one by one each time you enter the trigger until they are all active, then do the action you wanna do.
Your answer
Follow this Question
Related Questions
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer
Trigger help! 0 Answers
Check if trigger is occupied 1 Answer
OnTriggerEnter Not Working 1 Answer
Collision problem in C# 4 Answers