Timed Actions

Okay in my game there is a ball and when you hit the ball it X2’s your score. My problem is I want it to do it over a 15 second time span and as of now it only does it instantly. How can I do this? Here is my code. Keep in mind I am very very new to Unity.

var X2 : String = "X2";
function OnCollisionEnter(collision : Collision)
{
    if (collision.gameObject.name == X2)
    {
		
		Move.playerScore += 100 * 2;
	
        Destroy(collision.gameObject); 
    }
}

It will depend on whether you want to take the fastest way to do it, or a longer, safer…

A fast solution :

  • Make a Javascript file called ScoreManager :
var score : float = 0.0;
var scoreModifier : float = 1.0;
var bonus2xTime : float = 15.0;

private var timerBonus : float;

function AddScore (value : float) {
	score += value * scoreModifier;
}

function Bonus2x () {
	timerBonus = bonus2xTime;
}

function Update () {
	if (0 < timerBonus) {
		// Decrease the timer according to time.
		scoreModifier = 2;
		timerBonus -= Time.deltaTime;
	} else {
		scoreModifier = 1;
	}
}

Thank you so much for your help I will test it in a bit and get back to you.

I never would have figured all that out alone.

Cheers.

I hate being a noob at this sigh… I can’t figure out how to get this code to work at all :frowning:

http://unity3d.com/support/documentation/ScriptReference/index.Common_Operations.html

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html