- Home /
Set Objects in array active
I have a respawn script where after a variable is set true in another script, a wait coroutine would start and the array would be set active. I keep getting the error Assets/Scripts/Player/Respawn.cs(23,13): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.GameObject[]' could be found. Are you missing an assembly reference?
I looked over the internet and couldn't find any solution to my problem.
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Respawn : MonoBehaviour {
public int Wait;
public GameObject[] gas;
public int currentIndex = 0;
private static bool Touch;
// Use this for initialization
void Start () {
gas = GameObject.FindGameObjectsWithTag("Gas");
}
IEnumerator Respawning()
{
yield return new WaitForSeconds(Wait);
gas.enabled = true;
}
// Update is called once per frame
void Update () {
Touch = Gas.Touched;
if (Touch)
{
Debug.Log("Touch");
foreach(GameObject _obj in gas)
{
StartCoroutine(Respawning());
}
}
}
}
Answer by dan_wipf · Jun 28, 2018 at 05:08 AM
well you could either make a for, or a foreach loop. you get the error because you can't enable a GameObject. you have to set it active(like GameObject.SetActive(true)).
//foreach loop
foreach(GameObject obj in gas) {
obj.SetActive(true);
}
// for loop
for(int i = 0; i < gas.Length; i++;) {
gas[i].SetActive(true);
}
Your answer

Follow this Question
Related Questions
How to set parents of all objects in an array? 1 Answer
Enable My Script on Spawn 0 Answers
accessing gameObject script from an array 3 Answers
Passing an array to a function by reference? 1 Answer
Help with for loop and arrays 3 Answers