Instantiate a single object at a set time

I want to instantiate a single object a set time after I collide with another object, and I want to instantiate a second object about two seconds after that, but I’ve been working most of today without managing it. Is it possible?

If I use the code below, then the instantiated objects get created alright, but several instances of the object are created for as long as the clock continues running. Even if I stop the clock at 13 seconds, loads of objects appear in my Hierarchy.

if (hit.gameObject.tag == "cubeMoveRight"  characterTimerRight >10){
Instantiate (Cuberight1);
}
if (hit.gameObject.tag == "cubeMoveRight"  characterTimerRight >12){
Instantiate (Cuberight2);
}

If I use the code below, then I do not get any instantiated objects created at all

if (hit.gameObject.tag == "cubeMoveRight"  characterTimerRight ==10){
Instantiate (Cuberight1);
}
if (hit.gameObject.tag == "cubeMoveRight"  characterTimerRight ==12){
Instantiate (Cuberight2);
}

Can anyone help please, or point me in the right direction.

Thanks.

The facts :

  • When you (player) collide with something (first object), two seconds after, an object (second object) is spawned.

Questions :

  • After the spawn, what happens ? Can we still spawn others objects ? Or is it limited to one object ? Or perhaps always limited to one object existing, so it can only spawn again after the existing is dead ?

  • What is your player ? A CharacterController ?

  • What is your object ? A collider ?

Do you want the second one to appear only if the objects are still colliding, or no matter what?

What is your player ? A CharacterController - Yes?
What is your object ? A collider - the object I’mm colliding with is a collider. The objects being created are not colliding.

Explanation. I’m creating something for someone who can’t read, and so can’t read game instructions.

When my FPC collides with a certain object, then a sound file starts to explain the game. After 10 seconds (to coincide with the sound) a symbol appears on the screen. After another couple of seconds a second symbol appears close to the first symbol. That’s what I want.

What I’m getting using the above code is either loads of symbols appearing, or no symbols appearing.

Hope the explanation helps.

So it only triggers once.

var hasBeenTriggered : bool = false;
var triggerCinematic : GameObject;

function OnControllerColliderHit (hit : ControllerColliderHit) {
	if (hit.gameObject == triggerCinematic) {
		// Do the stuff required
	}
}

For timers :

Thanks AkilaeTribe. I’m just checking out the WaitForSeconds info. Seems to be what I need. Thanks for pointing me in right direction.

I’d done loads of searches today, but non for “Wait”. Serves me right!

Thick or what? Still not got this!

I’m starting with this:

function OnControllerColliderHit (hit : ControllerColliderHit){
if (hit.gameObject.tag == "cubeMoveRight"){
audio.PlayOneShot (moveRight);
characterHit = true;
soundPlayed = "moveRight";
}

Then I’ve tried both of these:

if (hit.gameObject.tag == "cubeMoveRight" ){
yield WaitForSeconds (10);
Instantiate (Cuberight1);
}
if (hit.gameObject.tag == "cubeMoveRight" ){
yield WaitForSeconds (10);
instance = Instantiate (Cuberight1);
}

And I’m finishing with:

function Update () {
if (characterHit) {
characterTimerRight += Time.deltaTime;
}
if (characterTimerRight > 14)
{
characterHit = false;
chracterTimerRight = 0.0;
}

Either of the bits of middle code create “Cuberight1” like I want, but it creates loads of them not just one of them!

Still stuck.

if (state == 0) {
yield WaitForSeconds (10);
instance = Instantiate (Cuberight1);
 state = 1;
  }

This also works … but also creates several instances in Hierarchy not just one.

Seems I’m beat.

Finally … Sorted.

Thank you, to those who pointed me in the right direction.

Sorted with a Coroutine

function Start (){
StartCoroutine ("CubeRight1");
yield WaitForSeconds (14);
StopCoroutine ("CubeRight1");
}
function CubeRight1(){
if (state == 0) {
yield WaitForSeconds (10);
instance = Instantiate (Cuberight1);
 state = 1;
if (state == 1) {
yield WaitForSeconds (2);
instance = Instantiate (Cuberight2);
 state = 2;
  }
}

Yield ensure your function will continue after the specified point (the yield position).

When you collide for the first time, it meets the yield but do not set state = 1 yet, that’s why it keep repeating detecting a collision with state == 0.

Put state = 1 right after if (state == 0), you are currently making your affectations too late.

Notice you may use a private boolean, instead of a int.