I have the following code:
public GameObject characterButtonPrefab;
public GameObject characterButtonContainer;
public GameObject optionsContainer;
public GameObject[] charButtons;
private int charButtonsIndex = 0;
private Text[] cBT;
private string[] buttonTexts = new string[] {"Strength 5", "Agility 5", "Intelligence 5", "Charisma 5", "Diplomacy 5" };
private void Start()
{
LevelIsOver = false;
IsNextWave = false;
//Time.timeScale = 1;
for (int i = 0; i < charButtons.Length; i++)
{
int size = charButtons.Length;
cBT = new Text[size];
GameObject container = (GameObject)Instantiate(characterButtonPrefab);
container.transform.SetParent(characterButtonContainer.transform, false);
cBT[i] = characterButtonPrefab.transform.FindChild("Text").GetComponent<Text>();
cBT[i].text = buttonTexts[Random.Range(0, buttonTexts.Length)];
charButtonsIndex++;
}
//if (charButtons.Length > 0)
//{
// GameObject currentChar = (GameObject)Instantiate(characterButtonPrefab);
// currentChar.transform.SetParent(optionsContainer.transform, false);
// currentChar.transform.SetSiblingIndex(0);
// currentChar.GetComponent<Button>().onClick.AddListener(() => CharacterSelection());
//}
}
I declared the size of array to 20 in the inspector and everything work fine. The buttons are created and text changed. The question is how do I duplicate the first button in that array to another panel ? I have searched the internet but nothing found about this.
I have tried this:
GameObject currentChar = (GameObject)Instantiate(charButtons[0]);
currentChar.transform.SetParent(optionsContainer.transform, false);
currentChar.transform.SetSiblingIndex(0);
currentChar.GetComponent<Button>().onClick.AddListener(() => CharacterSelection());
}
But I get ArgumentException: The Object you want to instantiate is null.
CharacterButtonPrefab?? \$\endgroup\$