Instantiate my winning collider?

Hello - im new to unity and i need a bit of help on my scripting.

  • so i was thinking of activating a winning point by colliding with a box collider that should send me to another scene (winning scene).

But this box should spawn after 150seconds have passed, but it doesn’t want to.

I’ll post my code so far.(this is my spawning code)

var WinObject:GameObject;

function Update () {
}

if(Time.time<3){
Instantiate(WinObject, transform.position, transform.rotation);
}

and next i got my collider.
Here…

function Update () {
}

function OnTriggerEnter(collision:Collider){

if(collision.gameObject.tag==“Player”)
{
Application.LoadLevel (2);
}
}

My problem is it wont spawn the box.
right now it’s set to spawn under 3seconds, and that works.
but i need it to spawn my box after 150seconds.

Anyone got a clue?

-Thanks, this is my first post, aswell as my first unity project, hope it’s not too “noobish” asking for help.

thanks again :slight_smile:

Everybody’s a newbie at some point! :slight_smile: Questions like this are just fine.

There are a few ways you could code the timer. Here are two that come to mind.

public var var WinObject : GameObject;

private var fl_start_time : float;

function Start() {
    fl_start_time = Time.time; // Get the starting time
}

function Update () {
    // If 150 seconds have passed since the starting time, instantiate the WinObject
    if (fl_start_time + 150 < Time.time) {
        Instantiate(WinObject, transform.position, transform.rotation);
    }
}

Or you could use Unity’s “Invoke” function. I discovered it a few weeks ago. It executes a function automatically after a delay.

public var var WinObject : GameObject;

Invoke("doInstantiateWinObject", 150);

private function doInstantiateWinObject() {
    Instantiate(WinObject, transform.position, transform.rotation);
}

Disclaimer: I haven’t tested either of those. But the should work, either as-is or with minor tweaking.

Hello and welcome. Lots of new folks afoot today it seems. And check it out – you already got one JS answer and now one C# answer!

First off, take a look at the Scripting reference (under Unity->Help). There is a subheading there called Keeping Track of Time (as well as Coroutines).

What I see in your snip is that where you are checking here

if(Time.time<3){
Instantiate(WinObject, transform.position, transform.rotation);
}

is not inside any MonoBehavior method like Awake, Start, Update, FixedUpdate, or LateUpdate. So essentially it is firing when the class initializes (i.e., it will spawn that one cube instantly because of that) and because it’s not in any of those behavior methods it never gets checked again – which is why you don’t see it working regardless of the number of seconds.

Anyway, this is off the cuff, but will get you there along with the Scripting reference.

void Start()
{
   // fire off the coroutine
   StartCoroutine(WaitToSpawnCube());
}

void SpawnCube()
{
   // create your prefab instance here
}

IEnumerator WaitToSpawnCube()
{
   // return immediately and execute SpawnCube()
   // after 150 seconds:

   yield return new WaitForSeconds(150);
   SpawnCube();
}

Good luck with everything!

@BoredKoi Cool use of the coroutine and the WaitForSeconds!

Thank you for the quick responce!

Yes it seems to work, and thank you for the advice :)Scripting isnt my strong side, but im at my final exam on 3D college, where i study 3D graphic, and the teachers want us to make a mini-game.

So they just randomly put us into, Unity! - Thank god i know how to make models and stuff, but on the otherside, i’ve spent nights trying to teach myself a little scripting now. Thanks for the help :slight_smile:

Cheers!