How do you set Prefabs into a GameObject array that will activate/deactivate when called?
Hi everyone, got a question - hope someone can help. I stored 3 different prefabs into my GameObject. On Start() I set them to be false. Then I randomly turn one on at a time, in a Coroutine also found in the Start(). However, when I try to run the game, nothing shows up on the screen. Any help would be must appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
// When initializing GameObjects make sure path to prefabs are set properly
public GameObject[] moles = new GameObject[3];
// Use this for initialization
void Start () {
//moles = Resources.LoadAll("Enemy") as GameObject[];
foreach (GameObject i in moles)
{
i.SetActive(false);
}
StartCoroutine(Gameloop());
}
IEnumerator Gameloop()
{
yield return new WaitForSeconds(1f);
yield return StartCoroutine(RandomMoleSpawn());
yield return StartCoroutine(EndSequence());
}
//Mole generator...
private IEnumerator RandomMoleSpawn()
{
int loop = 0;
//My infinite loop...
while (loop < 1)
{
int Index = Random.Range(0, moles.Length);
moles[0].SetActive(OnOff());
yield return new WaitForSeconds(0.5f);
}
}
//The ON and OFF switch for my moles...
public bool OnOff()
{
int Index = Random.Range(0, 3);
if (Index > 0)
{
return true;
}
else
{
return false;
}
}
private IEnumerator EndSequence()
{
Reset();
Debug.Log("Finished!");
yield return new WaitForSeconds(1f);
}
//Returns everything back to original state...
public void Reset()
{
foreach (GameObject i in moles)
{
i.SetActive(false);
}
}
// Update is called once per frame
void Update () {
}
}
Answer by KoenigX3 · Jan 06, 2017 at 10:35 PM
If you have assigned prefabs to it, you will see no change on the screen. You have to assign gameobjects from the scene to the 'moles' array, or instantiate them at runtime, and assign them to the array.
Right now you are changing the active state of the prefab, and not the objects on the screen (if you have placed any objects into the scene).
(Note: you can actually see the prefab's state in the Project window as the code executes, as it changes the first prefab)
EDIT: I've added a code which instantiates the 3 prefabs with 5 units distance on the X dimension.
public GameObject[] prefabMoles = new GameObject[3];
GameObject[] moles = new GameObject[3];
void Start () {
for(int i = 0; i < prefabMoles.Length; i++)
{
moles[i] = (GameObject) GameObject.Instantiate(prefabMoles[i], new Vector3((i-1)*5, 0, 0), Quaternion.identity);
}
}
Note that i have changed the name of the prefab array to 'prefabMoles', and the array of the spawned gameobjects is named 'moles' - which means that the rest of the code will modify the 'moles', which are spawned on the scene.
Your answer
Follow this Question
Related Questions
Character Selection 0 Answers
[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer
How to add a prefab menu to inspector in a Gameobject? 0 Answers
How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers
Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers