Trying to create an array of dead enemies to enable trigger.

Hi,

I’ve got a sphere that acts as a trigger to load the next level of the game.

There are 3 enemies: eye, eye2 and eye3.

I only want the sphere to appear and be able to work after the 3 enemies have been killed by the player.

If anyone could please look at the code I was trying to attach to the sphere/trigger and see where I’m going wrong that would be awesome.

using UnityEngine;
using System.Collections;

public class SphereScript : MonoBehaviour {

    GameObject eye;
    GameObject eye2;
    GameObject eye3;
    GameObject Sphere;
   
    void Start () {
        eye = GameObject.Find ( "eye" );
        gameObject.active = true;
        eye2 = GameObject.Find ( "eye2" );
        gameObject.active = true;
        eye3 = GameObject.Find ( "eye3" );
        gameObject.active = true;
        Sphere = GameObject.Find ( "Sphere" );
        gameObject.active = false;
        GameObject[] eyes;
        eye = eyes[0];
        eye2 = eyes[1];
        eye3 = eyes[2];
        }
   

    void Update () {

        if (eyes.gameObject.active = false) 
             {
                Sphere.gameObject = true;
            }
        }

    void OnTriggerEnter () {
   
        Application.LoadLevel ("Mockron_Level3");
       
    }

}
using UnityEngine;
using System.Collections;
public class SphereScript : MonoBehaviour {
    GameObject eye;
    GameObject eye2;
    GameObject eye3;
    GameObject Sphere;
 
    void Start () {
        eye = GameObject.Find ( "eye" );
        gameObject.active = true;
        eye2 = GameObject.Find ( "eye2" );
        gameObject.active = true;
        eye3 = GameObject.Find ( "eye3" );
        gameObject.active = true;
        Sphere = GameObject.Find ( "Sphere" );
        gameObject.active = false;
    }
 
    void Update () {
        if (eye.activeSelf == false && eye2.activeSelf == false && eye3.activeSelf == false) {
                Sphere.gameObject = true;
        }
    }

    void OnTriggerEnter ()  {
        Application.LoadLevel ("Mockron_Level3");
    }
}

This should work better i have not tested it so if you get any compile errors post them here.

Also in programming one always starts counting from 0 so to make this easier to read for a programmer you would do eye0 eye1 eye2

I just make an array for GameObjects and add a counter for them, when the count reaches your goal, then LoadLevel

Sorry didn’t see mathias234 post,

Yeah that would be the more “Scalable” version since if you are going to have more then 3 - 4 enemies it will be a lot of if(…)

“active” is deprecated- if you’re trying to read the value either use “activeInHierarchy” or “activeSelf”, but note that there’s a difference between them in that the former will return false if the parent is disabled, while the latter only turns false if the object itself is explicitly disabled (this makes a big difference).

If you’re trying to set the value, then use “SetActive(true/false)”.

Also, if the current object is disabled, Update doesn’t run, so the approach is wrong. Also “Sphere.gameObject=true;” is trying to assign a bool value to a GameObject. Just saying ^_~.

Hi, thanks everyone for their time.

You magnificent people!
I got it working. Mathia’s code worked very well, but as Lysander pointed out, if the current object is disabled the Update wouldn’t run, so I ended up putting the code on another game object.
Which at first disappeared when I started the scene, so I had to make sure I said Sphere.active = false in the start.

Anyway, I’ll post the code as it ended up. Many thanks again!

using UnityEngine;
using System.Collections;
public class SphereScript : MonoBehaviour {
    public GameObject eye;
    public GameObject eye2;
    public GameObject eye3;
    public GameObject Sphere;
   
    void Start () {
        eye = GameObject.Find ( "eye" );
        gameObject.active = true;
        eye2 = GameObject.Find ( "eye2" );
        gameObject.active = true;
        eye3 = GameObject.Find ( "eye3" );
        gameObject.active = true;
        Sphere = GameObject.Find ( "Sphere" );
        Sphere.active = false;
    }
   
    void Update () {
        if (eye.activeSelf == false && eye2.activeSelf == false && eye3.activeSelf == false) {
            Sphere.active = true;
        }
    }
}
   
//    void OnTriggerEnter ()  {
    //    Application.LoadLevel ("Mockron_Level3");
//    }
//}