Activating GameObjects one by one

Hi, I’m trying to come up with a script that will activate one of two cubes upon the player clicking. so at first there are no cubes visible, then you click and one (lilCube) becomes visible, then you click again and the second (bigCube) becomes visible. The code I have right activates both instantly, with only one click, even though I’ve tried adding a variable for how many times you’ve clicked. I’m not sure what would be an efficient way to check if the first cube is active yet, and if not, activate it, and then if it is, activate the second. Here is my code:

public GameObject lilCube;
public GameObject bigCube;

int clickTimes = 0;

void Start ()
{
	lilCube.SetActive(false);
	bigCube.SetActive (false);
}

void Update()
{
    if (Input.GetButton ("Fire1") && lilCube.activeInHierarchy == false && clickTimes == 0) 
    {
	    lilCube.SetActive (true);
	    clickTimes = 1;
     }

	if (Input.GetButton ("Fire1") && lilCube.activeInHierarchy == true && clickTimes == 1)
	{
		bigCube.SetActive (true);
		clickTimes = 2;
	}
}

Also, if I get this working, what would be an efficient way to do it for 10 cubes or more without making a cube prefab? Prefabs won’t work for what I’m using this script for. Would I just have to keep doing more and more complicated if statements? there has to be a better way. I’m new to coding. sorry.

I think the problem is that you dont use “else if” instead of “if” in the second check. The code is executed line by line. So you code right now first check if the amount of clicks is 0 then when that is true you se the amount of clicks to be 1, the directly after u check if the amount of clicks is 1 and this is now ture because you just set it to be.

So simply change the second if statement to “else if”.

The following script works for any number of cubes you want, as long as you know that number up front. If you don’t know up front the number of cubes, you can use ArrayList with some tweaking.

using UnityEngine;

public class ActivateCube : MonoBehaviour {

    //Create an array to hold any number of cubes you want
    public GameObject[] cubeArray = new GameObject[10]; //initialize the array for e.g. 10 cubes

    //Holds the index of the cubeArray, which corresponds to the next cube to be activated
    private int activateNext = 0;


	// Use this for initialization
	void Start () {

	    for (int i = 0; i < cubeArray.Length; i++)
	    {
	        cubeArray*.SetActive(false);*
  •  }*
    
  • }*

  • // Update is called once per frame*

  • void Update () {*

//whenever a click occurs, and as long as activateNext is less than the length of the array…

  •  if (Input.GetButtonDown("Fire1") && activateNext < cubeArray.Length)*
    
  •  {*
    

//… activate next cube

  •      cubeArray[activateNext].SetActive(true);*
    
  •      activateNext++;*
    
  •  }*
    
  • }*
    }