- Home /
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.
Answer by pako · May 23, 2015 at 06:48 AM
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[i].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++;
}
}
}
This is fantastic!! thanks! I didn't know how to make an array lol but this works great.
Answer by Snawws · May 22, 2015 at 07:37 PM
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".
This didn't change anything. I now have:
if (lilCube.activeInHierarchy == true)
{
if (Input.GetButton ("Fire1") && clickTimes == 1)
{
bigCube.SetActive (true);
clickTimes = 1;
}
}
else if (lilCube.activeInHierarchy == false)
{
if (Input.GetButton ("Fire1") && clickTimes < 1)
{
lilCube.SetActive (true);
clickTimes = 1;
}
}
and it still does the same thing
Use GetButtonDown ins$$anonymous$$d of GetButton. GetButton is for dragging and is called every update when the mouse button is down. GetButtonDown is only called once and is not repeted until it is click again. This happens so fast that it feels instant. I hope this is the answere to you problem. :)
Your answer
Follow this Question
Related Questions
Check if the child of an object is active not working!!! 2 Answers
Distribute terrain in zones 3 Answers
how to know if GameObject is active without getting warning? 2 Answers
Script only works onetime 0 Answers
Code Not Working 1 Answer