Ok so I'm trying to do what @Nypyren suggested by passing the PickUpManager to the FastBall to call the StartCoroutine from that, but I'm not entirely sure how to do it. I tried several different things but keep going back to my original set up because I feel like I'm just making a mess of my code and don't know what I'm actually doing. Here is my complete PickUpManager code:
private int numOfShots = 4;
private int bulletsPerShot = 7;
private BasePickUp pickUp;
private FastBall speedBall = new FastBall(); //This is used to access methods related to the different speed ball pick ups, I really need to find a different way to manage this though.
public List<Ball> allBalls = new List<Ball>();
public bool pickUpActive = false;
public List<BasePickUp> pickUpQueue = new List<BasePickUp>();
// Use this for initialization
void Awake () {
FindObjectOfType<Ball>().mainBall = true;
allBalls.Add(FindObjectOfType<Ball>());
}
// Update is called once per frame
void Update ()
{
if (pickUpQueue.Count != 0 && !pickUpActive)
{
pickUpActive = true;
pickUpQueue[0].ActivatePickUp();
}
}
public void AddToQueue (BasePickUp pickUp)
{
BasePickUp newPickUp = (BasePickUp)pickUp.GetComponent<BasePickUp>().Clone();
pickUpQueue.Add(newPickUp);
}
And here is the BasePickUP and FastBall code:
protected int AmmoRemaining = 35;
protected int timer = 5;
protected Ball mainBall;
void Start ()
{
//find the ball and assign it to this variable
foreach (Ball ball in GameManager.pickUpManager.allBalls)
{
if (ball.mainBall)
{
mainBall = ball;
}
}
}
void Awake ()
{
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
}
void OnTriggerEnter2D (Collider2D other)
{ //if brick is at end of screen, game over
if (other.name == ("LoseCollider"))
{
Destroy(gameObject);
}
}
public void OnCollisionEnter2D (Collision2D collision)
{
Vector2 tweak = new Vector2 (UnityEngine.Random.Range(0f, 0.2f),UnityEngine.Random.Range(0f, 0.2f));
this.gameObject.GetComponent<Rigidbody2D>().velocity += tweak;
//if the pickup makes contact with the paddle or ball....
if (collision.gameObject.tag == "Paddle" || collision.gameObject.tag == "Ball")
{
GameManager.pickUpManager.AddToQueue(this); //Add the pick up to the queue of pick ups.
Destroy(gameObject); //...and finally destroy power up object
}
}
abstract public void ActivatePickUp();
//Coroutine used to set ball speed back to normal when pick ups are deactivated
public IEnumerator PowerUpTimer (int delay)
{
yield return new WaitForSeconds (delay);
EndSpeedBall();
}
public void EndSpeedBall()
{
mainBall.Speed = 1;
mainBall.gameObject.GetComponent<Rigidbody2D>().velocity = mainBall.gameObject.GetComponent<Rigidbody2D>().velocity.normalized;
mainBall.gameObject.GetComponent<Rigidbody2D>().velocity *= mainBall.Speed*mainBall.MaxSpeed;
Destroy (GameManager.pickUpManager.pickUpQueue [0]);
GameManager.pickUpManager.pickUpQueue.RemoveAt (0);
GameManager.pickUpManager.pickUpActive = false;
}
public void ManageAmmo ()
{
AmmoRemaining -= 7;
if (AmmoRemaining <= 0)
{
Destroy (GameManager.pickUpManager.pickUpQueue [0]);
GameManager.pickUpManager.pickUpQueue.RemoveAt (0);
GameManager.pickUpManager.pickUpActive = false;
}
}
//use this to begin the coroutine
public void StartCoroutine (int delay)
{
StartCoroutine(PowerUpTimer(delay));
}
public object Clone ()
{
BasePickUp newPickUp = (BasePickUp)this.MemberwiseClone();
return newPickUp;
}
public override void ActivatePickUp ()
{
mainBall.GetComponent<Ball>().Speed = 2.0f; //increase ball speed...
StartCoroutine(timer); //...set time that power up is active
}
Can you help me understand what I need to do to pass the PickUpManager into the Fastball (or BasePickUp if that would be better) and what to do with it once it's there.