- Home /
Locker system
This is the code I have right now to get a locker system. If a button is pressed, currentCat is incremented, and i want the next cat to show, and the previous one to hide, but its not working
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LockerManager : MonoBehaviour { public GameObject[] Cats;
private int currentCat = 0;
public void NextCat()
{
currentCat += 1;
}
public void PreviousCat()
{
currentCat -= 1;
}
private void Update()
{
print(currentCat);
getCat();
if (currentCat > Cats.Length)
{
currentCat = 0;
}
if(currentCat < 0)
{
currentCat = Cats.Length;
}
}
void getCat()
{
for(int i = 0; i < Cats.Length; i++)
{
Cats[i].SetActive(true);
}
for (int i = 0; i > Cats.Length; i--)
{
Cats[i].SetActive(false);
}
}
}
Comment
Answer by Hellium · May 07, 2019 at 07:39 PM
public class LockerManager : MonoBehaviour
{
public GameObject[] Cats;
private int currentCatIndex = 0;
public void SelectCat( int index )
{
if( index < 0 || index >= Cats.Length )
return ;
Cats[currentCatIndex].SetActive( false );
currentCatIndex = index;
Cats[currentCatIndex].SetActive( true );
}
public void NextCat()
{
SelectCat( (currentCatIndex + 1) % Cats.Length );
}
public void PreviousCat()
{
SelectCat( (currentCatIndex - 1 + Cats.Length) % Cats.Length );
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612200914im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to determine string location inside a Text 2 Answers
How to get an object to follow the Slider knob? 0 Answers
C# UI Text not updating 1 Answer