destroy all game objects that are touching the same collider?
Okay let me set this up for you guys. I have a bowl and I have tennis balls that slowly fall into the bowl.
I have scripts set up on the bowl and the balls so that it can count how many tennis balls have entered the bowl. I want to now create a script that every 30 seconds, the tennis balls get counted and if there are 8 or less balls in the bowl, destroy all balls (essentially empty the bowl) BUT if there are 9 or MORE balls in the bowl, it's game over (bowl overflow)
here's the scripts that I have attached to the Ball:
public int Balltocount;
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Bowl")
{
other.gameObject.GetComponent<BallCounter>().CountBalls(Balltocount);
}
}
} And this is what I have on the BOWL:
public int Counter;
public void CountBalls(int howmany)
{
Counter -= howmany;
}
}
this works fine and is able to count the balls colliding in the bowl, its just the next step that i mentioned above that i cant figure out. Thanks to anyone who takes a look at this.
Answer by Obsessi0n · Feb 01, 2018 at 08:38 PM
This should work atleast the logic is correct.
public class ballcounter : MonoBehaviour {
public int totalballs =0;
private GameObject bola;
private GameObject[] allballs;
private int i = 0;
public void CountBalls(GameObject bola)
{
totalballs++;
allballs[i] = bola;
i++;
}
private void Start()
{
allballs = new GameObject[100];
StartCoroutine(Destroy());
}
IEnumerator Destroy()
{
while (true)
{
yield return new WaitForSeconds(5f);
if (totalballs > 8)
{
//gameover
}
else if (totalballs <= 8 && totalballs > 0)
{
for(int j =0;j< allballs.Length; j++)
{
if(allballs[j] != null)
{
Destroy(allballs[j].gameObject);
}
}
i = 0;
totalballs = 0;
}
}
}
}
public class ball : MonoBehaviour {
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Bowl")
{
other.gameObject.GetComponent<ballcounter>().CountBalls(this.gameObject);
Destroy(this.gameObject.GetComponent<ball>());
}
}
}
Corrected syntax and tried on my project and worked just fine.
Just want to say thank you for this answer! Sorry if I'm a little slow, I'm new to all of this. I'm still getting a NullReferenceException:Object reference not set to an instance an object on Line 34. I have no idea why this is happening.
hmm Im not getting it in my project I will look into it later. No problem I am also still learning :)
Copy the code again. I changed a few things it should no longer give NullReferenceException
It works and quite brilliantly at that. Thanks again, I've learned a lot and I sincerely appreciate your time and effort!
Hey man, if you would be so kind, I have one more quick question: I have a gameobject "grenade" in my game that gets thrown in the Bowl and destroys all the balls in the bowl. I would like this grenade to also set the counter back to "0" since all the balls are out of the bowl. I've tried adding OnCollisionEnter () into the script, I've also tried creating a new function but nothing seems to reset the counter back to "0" after the grenade goes off. I really hate to bother you but you're my only hope at this point! thank you sir.
Can you create a new question with your code? I will look into it and try to help.
Your answer
Follow this Question
Related Questions
How do you add +1 to a score counter after a destroy script (C#)? 1 Answer
How do I get a "counter" to reset back to "0" 2 Answers
How do I switch scenes with countdown timer and Score at the same time 0 Answers
Score counter system 2nd script and ball a little problem 1 Answer
How do I stop a scene from switching from switch before the Countdown timer reaches zero 2 Answers