Creating an up and down counter

Hi i am trying to make a counter that counts 70 and down to 0 and up to 70 again and so on. The use for the counter is that i have canon the shoots on touch, the speed of the ball is the current value of the counter. This the code that i have so far:

using UnityEngine;
using System.Collections;

public class Pukanje : MonoBehaviour {
	
	public GameObject proektil;
	public float fireRate = 0.5f;
	internal float nextFire;
	public int speed;
	public AudioClip weaponFX;
	
	public GUIText brzina;
	
	
	
	// Update is called once per frame
	void Update () 
	{
		speed = (int)Time.time;
		
		
			int max = 120;
			
			if(speed > 60)
			{
			speed = max - (int)Time.time;
			
				
			}
			if (speed < 0)
			{
				speed = Mathf.Abs(speed);
			}
		
		
		
		brzina.text = speed.ToString();
		// Fire is the left mouse button
		
		if (Input.GetButtonUp ("Fire1") && Time.time > nextFire) 
		{
			nextFire = Time.time + fireRate;
			GameObject clone = Instantiate(proektil,transform.position,transform.rotation) as GameObject;
			
			clone.rigidbody.velocity = transform.TransformDirection(-speed,0,0);
			
			audio.PlayOneShot(weaponFX);
			
			DestroyObject(clone,5);
		}
	}
}

The speed variable stores time.time value. Than it checks if it is greater than 60 if it is it begins counting backwards by subtracting from the max variable. And to go up again takes the absolute value of the now negative values of the counter. But than just keeps going up. The rest of the script is the fire function. Thanks in advance for any help.

Something like this should work…

float speed = 0;
float direction = 1;

speed += Time.deltaTime * direction;
if (speed < 0 || speed > max)
{
  direction = -direction;  // reverse direction
  speed = Mathf.Clamp(speed, 0, max); // make sure we stay within the bounds
}

Basically, use a direction variable that keeps track of the direction, either 1 or -1. When adding a value to speed, always multiply that value by direction so it’s positive or negative accordingly. When you get to one of the extents, reverse direction by negating it - i.e. direction = -direction.

Also, you’re using Time.time, which gives the time since the game started. You likely want to use Time.deltaTime instead.

I did it Unity has a function Mathf.PingPong.This is the solution

    public float timerce;
    public GUIText tekst;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
    {
       timerce = Mathf.PingPong(Time.time, 20);
       tekst.text = timerce.ToString();
	}