- Home /
Invoke Ball Obstacle based on Score
In the beginning of my game there is one instantiated bouncing ball which works great. As the player earns points up to 3 balls may be present on the screen at one time. I have three separate ball spawners in my scene. One is activated right away and the others activate once a certain score is reached. My issue is that up to 5 balls appear on the screen. I have been working on my code for hours trying to figure out a way to destroy a ball if there are already three visible on the screen. I am getting closer, but am still struggling. I was hoping someone might have a suggestion.
Here's my code - Used for Getting the Ball Moving:
using UnityEngine;
using System.Collections;
public class BallScriptLeft : MonoBehaviour {
private Animator anim;
private Rigidbody2D rb;
public GameObject ball;
void Start(){
anim = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
rb.velocity = new Vector2 (5f, 5f);
}
void Update(){
if (BallControllerLeft.bouncingBall == true) {
StartCoroutine (DestroyBall ());
}
if (ScoreManager.scoreCount >= 25000 && ScoreManager.scoreCount <= 50000) {
anim.speed = 1.2f;
}
if (ScoreManager.scoreCount >= 50000 && ScoreManager.scoreCount <=75000) {
anim.speed = 1.5f;
}
if (ScoreManager.scoreCount >= 75000 && ScoreManager.scoreCount <=100000) {
anim.speed = 1.8f;
}
}
void OnTriggerEnter2D(Collider2D target){
}
IEnumerator DestroyBall(){
yield return new WaitForSeconds (BallControllerLeft.bounceTime);
Destroy(ball);
BallControllerLeft.bouncingBall = false;
}
}
Code 2 - Used for Instatiating Ball:
using UnityEngine;
using System.Collections;
public class BallControllerLeft : MonoBehaviour {
public static int bounceTime;
public int spawnWait;
public Transform [] spawnPoints;
public GameObject ball;
private int ballCount = 0;
private int level=0;
public static int ballOnField=0;
public static bool bouncingBall=false;
void Start(){
}
void Update () {
if (ballCount == 1||BallBoxCollider.isDestroyed == true) {
StartCoroutine (OneBallAtATime ());
ballCount = 0;
BallBoxCollider.isDestroyed = false;
}
if (ScoreManager.scoreCount >= 25000 && ScoreManager.scoreCount <=50000 && level==0) {
level++;
bounceTime = 20;
spawnWait = 5;
InvokeRepeating("SpawnBall",spawnWait,spawnWait);
if (BallBoxCollider.isDestroyed == true) {
StartCoroutine (OneBallAtATime ());
level = 0;
}
//Invoke ("SpawnBall", spawnWait);
//StartCoroutine (OneBallAtATime ());
}
if (ScoreManager.scoreCount >= 50000 && ScoreManager.scoreCount <=75000) {
bounceTime = 30;
}
if (ScoreManager.scoreCount > 75000) {
bounceTime = 40;
}
}
void SpawnBall(){
ballCount=1;
ballOnField++;
bouncingBall = true;
Debug.Log ("SpawnBall Active");
int spawnIndex = Random.Range (0, spawnPoints.Length);
//int objectIndex = Random.Range (0, ball.Length);
Instantiate (ball, spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
}
IEnumerator OneBallAtATime(){
Debug.Log ("Left Ball Spawned");
CancelInvoke ("SpawnBall");
yield return new WaitForSeconds (bounceTime);
Debug.Log ("Left Ball Spawning");
ballOnField = 0;
InvokeRepeating("SpawnBall",spawnWait,spawnWait);
}
}
Code 3 - Used for Destroying Ball:
using UnityEngine;
using System.Collections;
public class BallBoxCollider : MonoBehaviour {
public GameObject ball;
public static bool isDestroyed=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (BallController.ballOnField > 1||BallControllerLeft.ballOnField > 1||BallControllerTop.ballOnField > 1) {
Destroy (ball);
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
BallController.ballOnField = 0;
isDestroyed = true;
Destroy (ball);
Debug.Log ("Collision Detected");
}
}
}
Hi,
You could assign the ball to a gameobject while instantiating, so that you can easily destroy the exact ball.
eg:
GameObject ballPrefab1;
ballPrefab1 = (GameObject)Instantiate (ball, spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation );
So, to destroy the first ball you could call Destroy(ballPrefab1).
If you have more balls, you could create more gameObjects such as ballPrefab1,ballPrefab2.... or you could create array or arrayList .. Since you are using maximum of 3 balls its good not to use array.