Start commands when variable hits certain numbers

Hi,

So I have a variable called TAPS which counts the amount of taps a user does on a button. It is counting downwards from 100 to 0. I want to start commands when the TAPS variable hits a certain number. So say TAPS goes to 70, then I want to start an animation. And at 50 I want to destroy an object, etc.

What is the best way to do this? My first idea is an if else setup within my function Update, but how exactly would I do that? ( with unity script) . Is this the ideal way to do it?

if(TAPS == 70) {

		// Start command here
		
	} else if (TAPS ==50) {
	
	    // Start command here

        } else if (TAPS ==0) {

          // Start command here

}

You could use a dictionary, and delegates. You could do otherwise, but I think it’s a good occasion to see them. Also, I’ve never done it in unity script. I’m going to assume you don’t know about them, and I’m sorry if you do.

So, a dictionary is a list that allow you to choose the way to acces a value, with your own type (called a key). Here we’re going to use an integer for the keys, and a function for the values. That function is called a delegate (although it’s more obvious in C#). Here is what I have in mind. I use the key space instead of a button, if you don’t mind.

#pragma strict
// You need that to use dictionaries
import System.Collections.Generic;

// First parameter between the <> is the key type, second is the value type
// Replace [] by <>. Website doesn't me to use them ...
var myEventDictionary : Dictionary.[int, function()]();

// This one is the same as you
var TAPS : int = 0;

function Start () 
{
	// At start, we initialize the dictionary
	myEventDictionary = new Dictionary.<int, function()>();
	// We associate an integer with a function
	myEventDictionary.Add( 10, FunctionAt10 );
	myEventDictionary.Add( 30, FunctionAt30 );
	myEventDictionary.Add( 50, FunctionAt50 );
	myEventDictionary.Add( 70, FunctionAt70 );
}

function Update () 
{
	if( Input.GetKeyUp( KeyCode.Space ) )
	{
		TAPS++;
		
		// Each time TAPS is changed, we check if we have it in our dictionary.
		if( myEventDictionary.ContainsKey(TAPS) )
		{
			// If we do, we call the function associated with it. We make sure the is one first.
			if( myEventDictionary[TAPS] != null )
				myEventDictionary[TAPS]();
		}
	}
}

// Those are the functions called in Update
function FunctionAt70()
{	
	Debug.Log( "At 70" );
}
function FunctionAt50()
{	
	Debug.Log( "At 50" );
}
function FunctionAt30()
{	
	Debug.Log( "At 30" );
}
function FunctionAt10()
{	
	Debug.Log( "At 10" );
}