I cant reference Objects in an array. Int array works
Hello, i made an array and tried to make the array full of public game objects (Panel1,Panel2,...). But i cant include the Panel in the array. I can call the object in every other function but not in the array. An int array (ints) works perectly. I think a verry simple misstake. Thank you for your help
public GameObject Panel1;public GameObject Panel2;public GameObject Panel3;public GameObject Panel4;
public GameObject[] Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};
public int[] ints = new int[]{ 1, 2, 3, 4, 5 };
Answer by davedevN · Jan 17, 2018 at 07:59 PM
Looks like you can't do "new GameObject[]{Panel1,Panel2,Panel3,Panel4};" in an initializer. Do it in a method instead.,Don't know for sure, but you probably can't do that in an initializer. So move "Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};" to a Start or other script. In fact, if you are using Visual Studio, roll over "Panel1" with the red squiggle under, it will tell you can't do that and why.
Sadly that didnt work. But i found the solution myself. You new to make the the Panels static. Now you can use them in the array but you cant assign them to an object. So you need a few GetPanel vars not static. First you set now the Panels to GetPanels and everything works fine. But thank you for your help.
public GameObject GetPanel1; public GameObject GetPanel2; public GameObject GetPanel3; public GameObject GetPanel4;
public static GameObject Panel1;public static GameObject Panel2;public static GameObject Panel3;public static GameObject Panel4;
public GameObject[] PublicPanels;
void Start () {
Panel1 = GetPanel1; Panel2 = GetPanel2; Panel3 = GetPanel3; Panel4 = GetPanel4;
GameObject[] Panels= new GameObject[] { Panel1, Panel2, Panel3, Panel4 };}
Your solution is unorthodox unless you have some specific purpose other that to "just make it work". Why are you using the statics.?Buckslice had a $$anonymous$$or error in redeclaring Panels. This should work
public GameObject Panel1;
public GameObject Panel2;
public GameObject Panel3;
public GameObject Panel4;
public GameObject[] Panels;
void Start () {
Panels = new GameObject[] { Panel1, Panel2, Panel3, Panel4 };
for(int i = 0; i < Panels.Length; i++)
{
Panels[i].SetActive(false);
}
}
I now found another solution wich works perfectly without any static or somethig. Works similar to yours. But thank you for your help. Though you get the correct for your help xD
Answer by Buckslice · Jan 17, 2018 at 08:01 PM
Try just doing:
public GameObject[] Panels;
and then assign your panels in the inspector.
Or you could call this part of your code in the start function instead
void Start(){
public GameObject[] Panels = new GameObject[]{Panel1,Panel2,Panel3,Panel4};
}
Sadly that didnt work. But i found the solution myself. You new to make the the Panels static. Now you can use them in the array but you cant assign them to an object. So you need a few GetPanel vars not static. First you set now the Panels to GetPanels and everything works fine. But thank you for your help.
public GameObject GetPanel1; public GameObject GetPanel2; public GameObject GetPanel3; public GameObject GetPanel4;
public static GameObject Panel1;public static GameObject Panel2;public static GameObject Panel3;public static GameObject Panel4;
public GameObject[] PublicPanels;
void Start () {
Panel1 = GetPanel1; Panel2 = GetPanel2; Panel3 = GetPanel3; Panel4 = GetPanel4;
GameObject[] Panels= new GameObject[] { Panel1, Panel2, Panel3, Panel4 };}