Question by
elgrayso · May 14, 2017 at 08:43 PM ·
array of gameobjects
Using an Array to Disable Objects (each from different places in the heirarchy)
I have a few different objects in the heirarchy that make up the kitchen in my scene. I want them to disappear upon pressing the E key.
I Made an empty game object and script called "KitchenSwitch". I created an array and dragged the objects from the heirarchy to link the objects to the array.
However, when I press "E" to disable the array, the "KitchenSwitch" script/object disables itself! What am I missing?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KitchenSwitch : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Note: Drag and Drop the appropriate objects
public GameObject[] currentKitch;
void Update ()
{
// a loop that defines each individual item in currentKitch as currentObj
foreach (GameObject currentObj in currentKitch)
{
GetComponent<GameObject> ();
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log ("currentKitch objects should be off");
gameObject.SetActive(false);
}
}
}
}
Comment