- Home /
The question is answered, right answer was accepted
Objects Appearing
I have 36 objects in and I want them to appear one by one. The player should press a certain key on the keyboard and one object would show up. When it presses the same key, another object would show up and so on. Does anyone know how I would do that?
Are they already created in the scene and the gameObjects are disabled (which makes them invisible), or you want them to literally spawn when a button is pressed?
They are already created. I want them to not be visible and when you press the key, it would be visible
Answer by leftshoe18 · Dec 08, 2018 at 09:17 PM
You could put all of the objects into an array and disable them on game start and enable them again upon pressing a button.
Something like this:
[SerializeField] GameObject[] objects;
int index = 0;
void Start()
{
foreach(GameObject go in objects)
go.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) //change this to whatever you want for input
{
if (index < objects.Length) // to avoid indexing errors
objects[index].SetActive(true);
index += 1;
}
}
The index increments with each new object enabled so that it will enable the next one the next time you press space.
Because my game objects are named things like A1, B3, ...
Actually, I figured it out. Thank you so much for your help!!
No problem!
Sorry I didn't reply sooner. I was at work. lol
Follow this Question
Related Questions
Object Visibility 2 Answers
Hide Object in Editor Only 5 Answers
Unity 2019.4.9f1 - Toggle Show/Hide Gameobject with one keystroke. 1 Answer
on collision, show/hide other model scripting ? 1 Answer
Look at Object 1 Answer