Currently my code works perfectly fine(i have disabled a few things here to make it simple) when i enter playmode however whenever i make any changes to code(unrelated to anything in Button Manager) the script recompiles(as expected) but my buttons stop working and i have to fully stop the game and enter play mode again. this is causing my workflow to be extremely slow.
i suspect that this might be a recompiling specific problem with unity but i am not a 100% sure
the thing that i absolutely don't understand is that if my code works normally then why does it break if i recompile it during playmode
i am sorry i am making a dumb mistake. i am completely self taught and learning this stuff as a hobby
This is the code i am using
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
public class ButtonManager : MonoBehaviour
{
[SerializeField] GameObject ButtonPrefab;
[SerializeField] List<GameObject> ActionButtons;
[SerializeField] GameObject ButtonHolder;
TurnManager turnManager;
ReticalManager reticalManager;
MapManager mapManager;
MoveDictionaryManager moveDictionaryManager;
void Awake()
{
//Debug.Log("ButtonManager Awake!");
setVariables();
}
public void setVariables()
{
turnManager = this.GetComponent<TurnManager>();
reticalManager = this.GetComponent<ReticalManager>();
mapManager = this.GetComponent<MapManager>();
moveDictionaryManager = this.GetComponent<MoveDictionaryManager>();
}
public void clearButtons()
{
for (int i = 0; i < ActionButtons.Count; i++)
Destroy(ActionButtons[i]);
ActionButtons.Clear();
}
public void InstantiateButtons(List<String> listFromCDH)
{
clearButtons();
for (int i = 0; i < listFromCDH.Count; i++)
{
ActionButtons.Add(Instantiate(ButtonPrefab));//Just Instanting
//Setting Transforms
ActionButtons[i].transform.SetParent(ButtonHolder.transform, false);
ActionButtons[i].transform.localPosition = new Vector3(ActionButtons[i].transform.localPosition.x, -50 * i);
//getting TMP to assign Text
TMPro.TextMeshProUGUI TMPthis;
TMPthis = ActionButtons[i].transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>();
// getting cache for captured variables
//int captured = i;//no Longer needed
string listCDHTEXT = listFromCDH[i];//This is the cache now
// using variables to set text
TMPthis.text = listCDHTEXT;
ActionButtons[i].name = listCDHTEXT + " Button";
//Assigning On Click Functions
Button thisButton = ActionButtons[i].GetComponent<Button>();
thisButton.onClick.RemoveAllListeners();
thisButton.onClick.AddListener(delegate
{
//moveDictionaryManager.doAction(listCDHTEXT);
Debug.Log("Button clicked: " + listCDHTEXT);
});
}
}
}
and if you need the arguments i am passing this list
List<string> GetCharacterMoveList()
{
List<string> defaultMovesAvaliable;
defaultMovesAvaliable = new List<string>();
defaultMovesAvaliable.AddRange(new List<string>
{
"Move",
"Attack",
"FireBall",
"End Turn"
});
return defaultMovesAvaliable;
}
i have tried to cache the values like "i", "thisButton" and even created an "Action" assuming it might be a problem with the for loop but the problem seems to persist even when i don't use a for loop