I have an array of objects.
I go through an array and I want to activate my objects in a cycle: 1, 2, 3, 1, 2, 3… and so on.
The problem that I’m facing is that de-activated objects also disappear from my array.
And this is what happens:
http://giphy.com/gifs/l0O9z6PGHDgg3GGZO
The 2 cubes just disappeared and the script stopped.
Instead I want the 3rd cube to appear and 1 and 2 to disappear, then the 1st to appear and the 2 and 3 to disappear and so on.
Here’s the code.
using UnityEngine;
using System.Collections;
public class Тестирую : MonoBehaviour
{
public int num;
[SerializeField] public GameObject[] objs;
void Start()
{
objs = GameObject.FindGameObjectsWithTag("Player");
num = 0;
}
void Update()
{
if (Input.GetButtonUp("RightBumper"))
{
NextOne ();
}
}
void NextOne()
{
if (num >= objs.Length - 1)
num = 0;
else
num++;
foreach (GameObject obj in objs)
{
obj.SetActive(false);
}
objs[num].SetActive(true);
}
}