- Home /
How can I make this GameObjects array remain permanent when i activate?
Hello, I have an EmptyObject with a code (see below the text) that is linked to 3 more EmptyObjects. The idea is that when you start the level the first Emptyobject will be activated while the other 2 will be deactivated and when you restarted the level for the first time EmptyObject first and second will be activated while third not. Moreover, the second time that you restart the level all three EmptyObject will be activated.
Now, when I restart for the second time, the third EmptyObject is activated and the second EmptyObject is deactivated.
Thenks!
public AudioSource Audio;
public GameObject[] Level_M;
public int Min = 0;
// Use this for initialization
public void Start () {
Min = PlayerPrefs.GetInt("Min");
if (Min <= Level_M.Length)
{
Level_M[Min].SetActive(true);
Min = Min + 1;
}
PlayerPrefs.SetInt("Min", Min);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode .Space))
{
SceneManager.LoadScene ("AudioManager");
Debug.Log ("RESET!");
}
}
}
Answer by caneva20 · Aug 13, 2019 at 09:00 PM
Each time you restart your game, you're only activating ONE GameObject through your code.
If I understood correctly what you want, the solution would be to LOOP through your GameObject[]
until Min
and not only activate the GameObject at Min
.
Follows below a sample code that would do it.
int min = PlayerPrefs.GetInt("Min");
for (int i = 0; i <= min; i++) {
Level_M[Min].SetActive(true);
}
PlayerPrefs.SetInt("Min", min + 1);
Okey, i think i can understant the problem whith your solution i gona try it and i say you something.
Thenks!