David Rios
David Rios

Reputation: 161

How to set a variable back to zero after a certain period of time

So I'm trying to work on a Ping Pong game as a learning activity for Unity. I'm working on making a reset system for the ping pong ball if it gets stuck bouncing up and down for eternity.

I set up a bottom wall collider trigger and a top wall collider trigger to increment a contact variable by 1. For example, if the ball hits the top wall of the game the HitsTop variable will increment by 1.

Likewise for the bottom wall. My issue is, when the ball hits the bottom wall and top wall ten times it resets the ball position.

I want to add code that will reset the HitsTop and the HitsBottom variables after a certain period of time, say, 5 seconds.

Is this possible in C#?

My code is as such:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    public static int PlayerScore1 = 0;
    public static int PlayerScore2 = 0;
    public static int HitsTop = 0;
    public static int HitsBottom = 0;

    public GUISkin layout;

    Transform theBall;

    // Use this for initialization
    void Start () {
        theBall = GameObject.FindGameObjectWithTag("Ball").transform;
    }

    public static void Score (string wallID) {
        if (wallID == "rightWall")
        {
            PlayerScore1++;
        } else if (wallID == "leftWall") {
            PlayerScore2++;
        }

        if (wallID == "topWallTrigger")
        {
            HitsTop++;
        } else if (wallID == "bottomWallTrigger") {
            HitsBottom++;
        }
    }

    void OnGUI () {
        GUI.skin = layout;
        GUI.Label(new Rect(Screen.width / 2 - 150 - 12, 20, 100, 100), "" + PlayerScore1 + HitsTop);
        GUI.Label(new Rect(Screen.width / 2 + 150 + 12, 20, 100, 100), "" + PlayerScore2 + HitsBottom);

        if (GUI.Button(new Rect(Screen.width / 2 - 60, 35, 120, 53), "RESTART"))
        {
            PlayerScore1 = 0;
            PlayerScore2 = 0;
            HitsTop = 0;
            HitsBottom = 0;
            theBall.gameObject.SendMessage("RestartGame", 0.5f, SendMessageOptions.RequireReceiver);
        }

        if (PlayerScore1 == 10)
        {
            GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER ONE WINS");
            theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
        } else if (PlayerScore2 == 10)
        {
            GUI.Label(new Rect(Screen.width / 2 - 150, 200, 2000, 1000), "PLAYER TWO WINS");
            theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
        }
        if (HitsTop == 10) {
            theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver);
            HitsTop = 0;
        } else if (HitsBottom == 10) {
            theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver);
            HitsBottom = 0;
        }
    }
}

Upvotes: 1

Views: 884

Answers (1)

Sam Gersh
Sam Gersh

Reputation: 46

Use Time.deltaTime to decrement a variable.

float timer = 5;    

void Update()
{
  //This will decrement the timer's value by the time. Once this hits zero, the timer is reset to its original value.
  timer -= Time.deltaTime;
  if(timer <= 0)
  {
   //Call reset game function
    timer = 5;
  }
}

Also when you increment HitsTop or HitsBottom, reset the timer to 5.

Upvotes: 1

Related Questions