Pong: Powerup problems

Hello,

In the pong game I’m making, powerups appear every so often. If the ball hits them, something good happens the player that last him the ball, or something bad happens the other player. This “Status Effects” last for 10 seconds, and then the ball is returned to normal.

The powerups are set up as prefabs. They are composed of a gameobject cube with a .png texture, istrigger is selected so the ball doesn’t bounce off them. They each have a script that calls a method on the paddle to apply the effect. They are instantiating fine, and disappearing, as they should when the ball hits them.

The problem is, the effect isn’t always applied. The pattern seems to be that once one effect has been applied to the paddle (and worn off), subsequent effects aren’t applied.

Here’s the relevant code for the paddle.

using UnityEngine;
using System.Collections;

public class PlayerPaddle : MonoBehaviour 
{
	
	// Paddle Vars
	public int speed; 
	public int size;
	public AudioClip paddleHit;
	public GameObject ball;
	
	// Status Effect Vars
	public static bool hasStatusEffect;
	public static bool isShrunk;
	public static bool isLarge;
	public static bool isFast;
	public static bool isSlow;
	
	// Status Effect Timers
	private float effectDuration;
	private float currentEffectDuration;
	
	
	// Use this for initialization
	void Start () 
	{
		// Initialise Paddle Vars
		speed = 15;
		size = 5;
		name = "Paddle1";
		effectDuration = 10;
		currentEffectDuration = effectDuration;
		
		// Initialise Status Effects
		isShrunk = false;
		isLarge = false;
		isFast = false;
		isSlow = false;
		hasStatusEffect = false;
	}
	
	
	// Update is called once per frame
	void Update () 
	{
		
		
		/*
		 * 
		 *  NEXT CODE BLOCK HANDLES REMOVING STATUS EFFECTS
		 * 
		 */

		// Check if a status effect is affecting the paddle
		if(hasStatusEffect == true)
		{
			// If > 0, reduce time
			if(currentEffectDuration > 0)
			{
				currentEffectDuration -= Time.deltaTime;
			}
			
			// If < 0, set to 0
			if(currentEffectDuration < 0)
			{
				currentEffectDuration = 0;
			}
			
			// If 0, remove status effect
			if(currentEffectDuration == 0)
			{
				// If the paddle size is not regular
				if(isShrunk == true || isLarge == true)
				{
					Debug.Log ("PlayerPaddle: Size Reseting");
					
					// Get the transform's scale
					Vector3 scale = transform.localScale;
					
					// Set the y scale back to the original size
					scale.y = size;
					transform.localScale = scale;
					
					// Reset Status effect Booleans
					isShrunk = false;
					isLarge = false;
					hasStatusEffect = false;
				}
				
				if(isFast == true || isSlow == true)
				{
					// Return Paddle to original speed
					speed = 15;
					
					// Reset Booleans
					isFast = false;
					isSlow = false;
					hasStatusEffect = false;
				}
			}
		}	
	}
	
	
	/*
	 * 
	 *	NEXT TWO METHODS HANDLES APPLYING STATUS EFFECTS FROM POWER-UPS 
	 * 
	 */
	
	// Sets Paddle Speed
	public void SetPaddleSpeed(int newSpeed)
	{
		speed = newSpeed;
		hasStatusEffect = true;
		
		if(newSpeed == 11)
		{
			isSlow = true;
			Debug.Log ("PlayerPaddle.SetPaddleSpeed: Speed: " + speed + " / isSlow: " + isSlow + " statusEffect: " + hasStatusEffect);
		}
		
		if(newSpeed == 20)
		{
			isFast = true;	
			Debug.Log ("PlayerPaddle.SetPaddleSpeed: Speed: " + speed + " / isFast: " + isFast + " statusEffect: " + hasStatusEffect);
		}
	}
	
	
	// Sets Paddle Size
	public void SetPaddleSize(int newSize)
	{
		hasStatusEffect = true;
		
		// Set the status effect
		if(newSize == 3)
		{
			isShrunk = true;	
		}
		
		if(newSize == 7)
		{
			isLarge = true;
		}
		
		// Resize the Paddle
		Vector3 scale = transform.localScale;
		scale.y = newSize;
		transform.localScale = scale;
		
		Debug.Log ("PlayerPaddle.SetPaddleSpeed: Size: " + scale.y + " / isShrunk: " + isShrunk + " / isLarge: " + isLarge + " statusEffect: " + hasStatusEffect);	
	}
	
}

Here’s one of the powerup scripts (they are fairly similar).

using UnityEngine;
using System.Collections;

public class SizePlus : MonoBehaviour 
{
	
	/** INSTANCE FIELDS **/
	public PlayerPaddle p1Script;
	public EnemyPaddle p2Script;
	
	
	// Use this for initialization
	void Start () 
	{
		
		GameObject go = GameObject.Find("Paddle1");
		p1Script = (PlayerPaddle) go.GetComponent(typeof(PlayerPaddle));
		
		GameObject go1 = GameObject.Find("Paddle2");
		p2Script = (EnemyPaddle) go1.GetComponent(typeof(EnemyPaddle));
	}
	
	
	// Check for collisions
	void OnTriggerEnter(Collider other)
	{
		Debug.Log("Size +");
		
		// Find out which controller last hit the ball
		int lastPaddleHit = GameGui.lastHitPaddle;
		Debug.Log("Last paddle: " + lastPaddleHit);
		
		// THIS POWERUP HAS A POSITIVE EFFECT, SO IT AFFECTS THE PLAYER WHO LAST HIT THE BALL
		
		// If the last hit paddle is 1
		if(lastPaddleHit == 1)
		{
			Debug.Log("size + paddle 1");

			p1Script.SetPaddleSize(7);
			
			Destroy(this.gameObject);
			GameGui.powerUpInPlay = false;
			
		}
		
		if(lastPaddleHit == 2)
		{
			Debug.Log("size + paddle 2");
			
			p2Script.SetPaddleSize(7);
			
			Destroy(this.gameObject);
			GameGui.powerUpInPlay = false;
		}
	}
}

Sorry about all the code. I’ve been at this for several hours now and would really appreciate a little help! Thanks!

The problem was that the currentEffectDuration wasn’t being reset. Since it was 0, the effect was being applied and immediately reset. `

 // Check if a status effect is affecting the paddle
 if(hasStatusEffect == true)
 {
    // If 0, remove status effect
 if(currentEffectDuration == 0)
 {
  // If the paddle size is not regular
  if(isShrunk == true || isLarge == true)
  {
      Debug.Log ("PlayerPaddle: Size Reseting");

      // Get the transform's scale
      Vector3 scale = transform.localScale;

      // Set the y scale back to the original size
      scale.y = size;
      transform.localScale = scale;

      // Reset Status effect Booleans
      isShrunk = false;
      isLarge = false;
      hasStatusEffect = false;
  }

  if(isFast == true || isSlow == true)
  {
      // Return Paddle to original speed
      speed = 15;

      // Reset Booleans
      isFast = false;
      isSlow = false;
      hasStatusEffect = false;
  }
 }`