- Home /
Scripts of instantiated objects
I am making a game such that the enemies are instantiated from enemy prefab and we destroy them by clicking them. Now, the problem arises when I want to know how many enemies i've destroyed. I put a script on the prefab for destroying and counting the number of enemies destroyed. But, the script is seperated into each instantiated object and it couldn't count the total destroyed objects.
Here's the script::
var speed : float;
var explosion : Transform;
function Update() {
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
function OnTriggerEnter(collision : Collider){
Destroy(gameObject);
}
function OnMouseDown(){
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
The function OnMouseDown is where I think the counting should take place.
Any help would be greatly appreciated...
**Sorry for my Bad English :)
Answer by DaveA · Aug 13, 2013 at 06:05 PM
var speed : float;
var explosion : Transform;
static var count = 0;
function Update() {
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
function OnTriggerEnter(collision : Collider){
Destroy(gameObject);
}
function OnMouseDown(){
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
count++;
}
'static' makes it share among all instances.
Thanks very much... You surely helped me get out of this terrible mess!
Your answer
Follow this Question
Related Questions
Tagging object while instating not working. 3 Answers
How to access script variables attached to a prefab at runtime in Javascript? 2 Answers
Change assigned prefab from script 3 Answers
Get Component from Instantiated Prefab 1 Answer
Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers