Search Unity

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

Discussion in 'Scripting' started by cristo, Jul 28, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    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.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SphereScript : MonoBehaviour {
    5.  
    6.     GameObject eye;
    7.     GameObject eye2;
    8.     GameObject eye3;
    9.     GameObject Sphere;
    10.    
    11.     void Start () {
    12.         eye = GameObject.Find ( "eye" );
    13.         gameObject.active = true;
    14.         eye2 = GameObject.Find ( "eye2" );
    15.         gameObject.active = true;
    16.         eye3 = GameObject.Find ( "eye3" );
    17.         gameObject.active = true;
    18.         Sphere = GameObject.Find ( "Sphere" );
    19.         gameObject.active = false;
    20.         GameObject[] eyes;
    21.         eye = eyes[0];
    22.         eye2 = eyes[1];
    23.         eye3 = eyes[2];
    24.         }
    25.    
    26.  
    27.     void Update () {
    28.  
    29.         if (eyes.gameObject.active = false)
    30.              {
    31.                 Sphere.gameObject = true;
    32.             }
    33.         }
    34.  
    35.     void OnTriggerEnter () {
    36.    
    37.         Application.LoadLevel ("Mockron_Level3");
    38.        
    39.     }
    40.  
    41. }
     
  2. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class SphereScript : MonoBehaviour {
    5.     GameObject eye;
    6.     GameObject eye2;
    7.     GameObject eye3;
    8.     GameObject Sphere;
    9.  
    10.     void Start () {
    11.         eye = GameObject.Find ( "eye" );
    12.         gameObject.active = true;
    13.         eye2 = GameObject.Find ( "eye2" );
    14.         gameObject.active = true;
    15.         eye3 = GameObject.Find ( "eye3" );
    16.         gameObject.active = true;
    17.         Sphere = GameObject.Find ( "Sphere" );
    18.         gameObject.active = false;
    19.     }
    20.  
    21.     void Update () {
    22.         if (eye.activeSelf == false && eye2.activeSelf == false && eye3.activeSelf == false) {
    23.                 Sphere.gameObject = true;
    24.         }
    25.     }
    26.  
    27.     void OnTriggerEnter ()  {
    28.         Application.LoadLevel ("Mockron_Level3");
    29.     }
    30. }
    31.  
    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
     
  3. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    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,
     
  4. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    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(.......)
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    "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 ^_~.
     
  6. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    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
    Code (CSharp):
    1. Sphere.active = false
    in the start.

    Anyway, I'll post the code as it ended up. Many thanks again!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SphereScript : MonoBehaviour {
    4.     public GameObject eye;
    5.     public GameObject eye2;
    6.     public GameObject eye3;
    7.     public GameObject Sphere;
    8.    
    9.     void Start () {
    10.         eye = GameObject.Find ( "eye" );
    11.         gameObject.active = true;
    12.         eye2 = GameObject.Find ( "eye2" );
    13.         gameObject.active = true;
    14.         eye3 = GameObject.Find ( "eye3" );
    15.         gameObject.active = true;
    16.         Sphere = GameObject.Find ( "Sphere" );
    17.         Sphere.active = false;
    18.     }
    19.    
    20.     void Update () {
    21.         if (eye.activeSelf == false && eye2.activeSelf == false && eye3.activeSelf == false) {
    22.             Sphere.active = true;
    23.         }
    24.     }
    25. }
    26.    
    27. //    void OnTriggerEnter ()  {
    28.     //    Application.LoadLevel ("Mockron_Level3");
    29. //    }
    30. //}
    31.