I'm working on a system for my game that will allow the player to stack pick ups in a queue. As one pick up expires, the next automatically activates. I'm having an issue though where if I pick up the first one, it activates fine, but if i pick up a second directly after it, it overrides the first one, activates the second one, and then once it has run it's course, everything goes back to normal gameplay, no first pick up. I'm not sure why this is happening. Hopefully someone can spot what I'm doing wrong in my code.
Here is the code for the pick up manager:
// Update is called once per frame
void Update ()
{
if (pickUpQueue.Count != 0 && !pickUpActive)
{
pickUpActive = true;
pickUpQueue[0].ActivatePickUp();
}
DeactivatePickUp();
}
void DeactivatePickUp ()
{
if (pickUpQueue.Count != 0 && pickUpActive)
{
Destroy (pickUpQueue [0]);
pickUpQueue.RemoveAt (0);
pickUpActive = false;
}
}
And here is the PickUp:
public override void ActivatePickUp ()
{
ball.GetComponent<Ball>().Speed = 2.0f; //increase ball speed...
ball.GetComponent<Ball>().StartCoroutine(timer); //...set time that power up is active
}
There is also a Base Pick Up:
public void OnCollisionEnter2D (Collision2D collision)
{
Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f),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")
{
GameObject.FindObjectOfType<GameManager>().GetComponent<PickUpManager>().pickUpQueue.Add(this);
Destroy(gameObject); //...and finally destroy power up object
}
}
As a side note, I am trying to find a solution to this that will work for all of my pickups. Some pickups are ammo based, some are timed.