Hi!

How can I instanciate an object (an empty object with its child objects) when a value of a global variable is reached in Javascript?.

The idea is that this object has an animation as a component object and I want instanciate this object during a short time and after the animation is watched, destroy the instanciated object.

Maybe with coroutines or there’s any other easier and/or direct way to implement this?

Thank you very much.

if(globalvar != lastglobalvarvalue)
{
var go = Instantiate(prefab, transform.position, transform.rotation);
var animationlenght = go.animation.clip.lenght;
Destroy(go, animationlenght);

}
lastglobalvarvalue = globalvar;

“mensaje” is the GameObject I want to instanciate.
“posicion mensaje” is the empty GameObject used as a position where I want to instantiate my “mensaje” GameObject.

I’m checking 24 times per second if activate is activated and when it’s activate I instantiate the object, my “mensaje” GamObject in the desired position.

Well, I can’t print any of my messages “bien” or “mal”.

I’ve created first script “global_counter_script” the global variables used in the other two scripts which modify them:

 static var globos_azules_explotados:int=0 ;
 static var globos_rojos_explotados:int=0 ;
 static var globos_amarillos_explotados:int=0 ;
 
 static var goal_azules: boolean;

And in the second script I’ve modified (incremented) the value of some global variables

 private var globos_rojos_explotados:int ;
 private var globos_amarillos_explotados:int ;
 private var globos_azules_explotados:int ;
 
 private var activate:boolean;

.
.
.

//after some instructions:
if (tipo_globo =="globo_normal_azul(Clone)") 
	{
	print("entro azul");
	Contador_global.globos_azules_explotados++; //acedemos a la variable global que cuenta el número de globos azules explotados
	if (Contador_global.globos_azules_explotados==1)
		{
		print("llegamos_a uno");
		activate=true;
		}
	}

I can print my messages in this, from second script using the global variables as you can watch, but I can’t evaluate the global variable in the third script as I’ve done in the second one.

And this is the script where I want to check the condition based in a global variable created in the first script.

var mensaje:GameObject;
var posicion_mensaje: Transform;
private var activate:boolean=false;


function update() 

{

 if (activate==true)
	{
	print("bien");
	var go=Instantiate(mensaje, transform.position, transform.rotation);
	}
	else
	{
	print("mal");
	}
}

Thank you very much in advance.