0
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why you need to duplicate the first button, is it different from others, can't you just instantiate new button with your CharacterButtonPrefab?? \$\endgroup\$ Apr 12 '19 at 12:37
  • 1
    \$\begingroup\$ The error message "The Object you want to instantiate is null" seems pretty self-explanatory to me. You seem to do this before you put the button into the button array. Or you have that code in a different object with a different charButtons array. \$\endgroup\$
    – Philipp
    Apr 12 '19 at 12:43
0
\$\begingroup\$

Supposing you have set the length of the charButtons in the inspector to 20, the default value of each element is null. You have to fill this array with instantiated buttons:

 int size = charButtons.Length;
 cBT = new Text[size];

 for (int i = 0; i < charButtons.Length; i++)
 {
     charButtons[i] = (GameObject)Instantiate(characterButtonPrefab);
     charButtons[i].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 = Instantiate(charButtons[0]);
     currentChar.transform.SetParent(optionsContainer.transform, false);
     currentChar.transform.SetSiblingIndex(0);
     currentChar.GetComponent<Button>().onClick.AddListener(CharacterSelection );
 }
\$\endgroup\$
1
  • \$\begingroup\$ Yes this is the answer I applied and it work. Thanks \$\endgroup\$
    – Dares
    Apr 13 '19 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.