Reputation: 37
I want to have one gameObject active. At the moment with "space" I can count from 1 to 12 and one single gameObject is active. Now I want it the other way round from 12-1 with "Esc" for example...
Thank you for your help!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ToggleTest : MonoBehaviour
{
public GameObject[] objects;
public int objCount = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
objCount += 1;
objects[objCount].SetActive(true);
objects[objCount - 1].SetActive(false);
}
}
}
Upvotes: 1
Views: 44
Reputation: 26460
Does the following work for you? You might want to erase the else
if you want to negate the effects on simultaneous pressing of space
and escape
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && objCount < 11)
{
objCount += 1;
objects[objCount].SetActive(true);
objects[objCount - 1].SetActive(false);
}
else if (Input.GetKeyDown(KeyCode.Escape) && objCount > 0)
{
objCount -= 1;
objects[objCount].SetActive(true);
objects[objCount + 1].SetActive(false);
}
}
Upvotes: 3