- Home /
Why the state of the member array of the class isn't maintained outside the function it is initialized in?
I have an array of GameObject inside the class which is initialized in a method and given certain number of elements inside that method, however when that particular array is accessed outside the class it's initialized in, the array turns out totally empty. Another thing is the certain variables are public and assigned in inspector, despite of all that the console warns of unassigned public variables and causes mess. Attached pictures would illustrate what i mean.
[1]: /storage/temp/45587-prob2.jpg
Can you tell us what lines it complains about unassigned public variables for? And what do you mean by causes mess?
I don't see the relation between the screenshots attached. Can you post the exact errors, as text? Use the 101010
button in the toolbar to post code.
Answer by Bitpocketer · May 01, 2015 at 11:13 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Data;
using Mono.Data.SqliteClient;
using UnityEngine.UI;
namespace Assets.Scripts
{
class sqlites : MonoBehaviour
{
String conn;
SqliteDataAdapter myDataAdapter;
SqliteCommand myCommand;
DataSet myDataset;
DataTable myDatatable;
// Parent referencing transform of Question Containter
public GameObject Parent=null;
//Questionpanel a child panel of parent transform
public GameObject QuestionPanel=null;
GameObject[] a;
// public GameObject button;
//counter for questions category in database
int obsession_categorycounter = 1;
string Query;
public GameObject next=null;
GameObject[] quest=null;
void Start()
{
FillQuestions(obsession_categorycounter);
}
public void FillQuestions(int counter)
{
//counter for categories
conn = "URI=file:" + Application.dataPath + "\\NueralPunctures";
Query = "Select * From ObsessionsQuestions where ObsessionsQuestions.Type="+counter.ToString();
myDataAdapter = new SqliteDataAdapter(Query, conn);
myDataset = new DataSet();
myDataAdapter.Fill(myDataset, "ObsessionsQuestions");
int Total_Questions=myDataset.Tables["ObsessionsQuestions"].Rows.Count;
Debug.Log(Total_Questions.ToString() + " Total questions");
quest = new GameObject[Total_Questions];
for (int i = 0; i < Total_Questions; i++)
{
quest[i] = (GameObject)Instantiate(QuestionPanel);
quest[i].transform.SetParent(Parent.transform, false);
quest[i].GetComponentInChildren<Text>().text = myDataset.Tables["ObsessionsQuestions"].Rows[i]["Question"].ToString();
Text txt = quest[i].transform.Search("Example");
txt.text = myDataset.Tables["ObsessionsQuestions"].Rows[i]["Example"].ToString();
}
GameObject mybutton = (GameObject)Instantiate(next);
mybutton.transform.SetParent(Parent.transform, false);
}
public void test()
{
Debug.Log(quest.Length.ToString());//this report 0 elements in console
for (int i =quest.Length; i >= 0;i--)
{
Destroy(quest[i].gameObject); // and this throws indexoutofrangeexception;
}
}
}
}
the variables QuestionPanel,next,Parent are assigned in inspector, the quest array is initialized in FillQuestions() method. The test() method is triggered by button, when this test() method is triggered it throws indexoutofrangeException,and debug.log(quest.length.Tostring()) shows 0 elements in quest.
Answer by jmgek · May 01, 2015 at 09:25 PM
***Class Child***
Public Child
{
public static int[] oneInt = {1,2,3};
}
***Child2.cs***
private void Main()
{
Child child1 = new Child();
DebugLog(child1.oneInt[0]);
}