Hi! Im a beginner and have a question. I couldn’t find a similar question.
I want to spawn and use (transform position, the design…) buttons in an array. I use a prefab of the button and have everything linked correctly (the prefab is linked to the script as ButtonPrefab).
public class LevelManager : MonoBehaviour
{
public int numButtons = 10;
public GameObject ButtonPrefab;
public Button[] ButtonMap;
// Start is called before the first frame update
void Start()
{
ButtonMap = new Button[numButtons];
for (int i = 0; i < numButtons; i++)
{
ButtonMap _= Instantiate(ButtonPrefab, new Vector3(i * 30f, i * 10f, 0), Quaternion.identity).GetComponent<Button>();_
//ButtonMap_.GetComponent().anchorMax = new Vector2(i * 10f, i * 10f);
//ButtonMap.GetComponent().anchorMin = new Vector2(i * 10f, i * 10f);
//ButtonMap*.name = “Button” + i.ToString();*_
if (ButtonMap == null)
{
Debug.Log("2 I am null " + i.ToString());
}
}
}
Now I get null everytime, how do I do this correctly? Thanks in advance!
SOLVED:
public class LevelManager : MonoBehaviour
{
public int numButtons = 10;
public GameObject ButtonPrefab;
public GameObject[] ButtonMap;
// Start is called before the first frame update
void Start()
{
ButtonMap = new GameObject[numButtons];
for (int i = 0; i < numButtons; i++)
{
ButtonMap = Instantiate(ButtonPrefab, new Vector3(i * 30f, i * 10f, 0), Quaternion.identity) as GameObject;
ButtonMap*.name = “Button” + i.ToString();*
//Debug
if (ButtonMap == null)
{
Debug.Log("2 I am null " + i.ToString());
}
}
}