- Home /
Missing references and incorrect values
So I have this really weird problem where I get incorrect values on a function called by a button and also missing references.
what is REALLY REALLYYYY WEIRD is that the variable is set on the inspector but in code the value is different, this has me really fustrated because it makes no sense
also the code is isolated within it's own namespace so there are no other scripts involved;
the problem lies in gameDatabase.actLevel that sets to null by itself (idk why or how). I tried to work around this by referencing the level by index but the indexLevel has an incorrect value when I call OprPress via button event
Data Classes
[System.Serializable]
public class Database
{
public Level[] levels;
public Level actLevel;
}
[System.Serializable]
public class Level
{
public string question;
public string[] options;
}
GameManager
public class TriviaQuestions : Minigame
{
const int SOLUTION = 0;
public Database gameDatabase;
public UI gameUI;
public Sound gameSound;
[System.NonSerialized] public int indexLevel;
public void OptPress(Text _pressed)
{
Debug.Log(JsonUtility.ToJson(gameDatabase.actLevel));
Debug.Log("game index is " + indexLevel);
// Debug.Log(_pressed.text + " " +gameDatabase.levels[indexLevel].options[SOLUTION]);
MG_End(_pressed.text == gameDatabase.actLevel.options[SOLUTION]);
}
public override void MG_Start(int _id)
{
indexLevel = _id;
Debug.Log("starting new game " + indexLevel);
gameObject.SetActive(true);
gameDatabase.actLevel = gameDatabase.levels[_id];
gameUI.actLevel = gameDatabase.actLevel;
Debug.Log(JsonUtility.ToJson(gameDatabase.actLevel));
}
}
UI Manager
[System.Serializable]
public class UI
{
[SerializeField] private Text textQuestion;
[SerializeField] private HolderCombo.HolderCombo answerInstantiate;
private List<GameObject> buttonOptions = new List<GameObject>();
private List<Text> textOptions = new List<Text>();
public Level actLevel
{
set
{
int _iLength, _vLength; // cache, evitar llamadas a count y bla bla bla
_iLength = textOptions.Count;
_vLength = value.options.Length;
// Si he instanciado menos opciones de las que necesito, instanciame más
while (_iLength < _vLength)
{
GameObject go = GameObject.Instantiate(answerInstantiate.Pprefab, answerInstantiate.Pholder);
buttonOptions.Add(go);
textOptions.Add(go.GetComponentInChildren<Text>());
_iLength++;
}
string[] _opt = (string[])value.options.Clone();
_opt.Shuffle();
// LLenando preguntas
for (int ii = 0; ii < _vLength; ii++)
{
textOptions[ii].text = _opt[ii];
buttonOptions[ii].SetActive(true);
}
// si tengo más de las que necesito instanciadas, apagar
for (int ii = _vLength; ii < _iLength; ii++)
{
buttonOptions[ii].SetActive(false);
}
textQuestion.text = value.question;
}
}
}
So when I run this, this is the output notice the actLevel is empty when the OptPress is called but not when we start the game even when we don't update it And also, in the editor we have this
Now when I use try to reference the level via index it works for the first game but the value is not updated on MB_Start call and we cant access the level we are in correctly.
So yeah I tried a lot of stuff and nothing seems to work. I started to think it's a unity bug.