Question by
Yonisit · Oct 04, 2018 at 05:14 PM ·
unity 5playerprefsfor-loopunity3.5
Loop function isn't updating
Hi there! In game, when using PlayerPrefs, the "for" loop isn't updating correctly, when I click the button, it should add 1 to the playerprefs, and it does, but the button intreactable isn't updating (The for loop in "InstantiateTrainB". I don't know why. Please help me ASAP :) THANKS!!
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public Button[] MineLevel;
public GameObject[] Trains;
public GameObject[] Mines;
public Text text;
public int CurrentTrainToSpawn=0;
private void Start()
{
}
private void FixedUpdate()
{
UpdateTrain();
}
public void InstantiateTrainB()
{
MineLevel[CurrentTrainToSpawn].gameObject.active = false;
Trains[CurrentTrainToSpawn].gameObject.active = true;
Mines[CurrentTrainToSpawn].gameObject.active = true;
PlayerPrefs.SetInt("MineCountA", CurrentTrainToSpawn + 1);
PlayerPrefs.Save();
CurrentTrainToSpawn++;
}
void UpdateTrain()
{
int MineCountReached = PlayerPrefs.GetInt("MineCountA", 1);
for (int i = 0; i < MineLevel.Length; i++)
{
if (i > MineCountReached)
{
MineLevel[i].interactable = false;
}
}
text.text = PlayerPrefs.GetInt("MineCountA").ToString();
}
}
Comment
"...The for loop in "InstantiateTrainB"."
I don't see a loop in InstantiateTrainB
@JVene I meant in "UpdateTrain()" Sorry. please help me if you can :)
THAT I see ;)
I need to figure out if setting interactable false is merely not showing a disabled status, so check and tell me if the buttons set to false are still responding to clicks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175756im_/https://answers.unity.com/themes/thub/images/avi.jpg)