- Home /
Return List from Singleton
I have set up a singleton class to handle all the database functions for my application. Now in order to show the data the user entered into the database, I'm using an instantiated prefab based on a class of gameobjects, in this case text. I'm trying to put the database data into a list so the prefab can be instantiated, but it keeps saying: Assets/Scripts/DatabaseManager.cs(171,16): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference? I know I added using System.Collections.Generic, so I'm not sure why it's saying that. Also, I'm not sure how to call the list to be created.
Do I need the using statements on both scripts? The singleton and the main script?
How do I call the list to be made and return it?
public void GetOilChangeInfo() { OilChangeList.Clear(); //Clear the list(not needed?) List<OilChange> OilChangeList = DatabaseManager.Instance.GetOilChangeList(); ShowOilChangeInfo(); //This function will display the items from the list and instantiate the prefab. }
On the Singleton:
public List GetOilChangeList()
{
mConnection.Open();
mSQLString = "SELECT * FROM " + SQL_TABLE_OIL_CHANGES;
mCommand.CommandText = mSQLString;
mCommand.ExecuteNonQuery();
mReader = mCommand.ExecuteReader();
while (mReader.Read())
{
OilChangeList.Add(new OilChange(mReader.GetString(0), mReader.GetString(1)));
Debug.Log(mReader.GetString(0) + mReader.GetString(1));
}
mReader.Close();
mConnection.Close();
return OilChangeList;
}
Thanks in advance...
Please format your code in the future. I fixed that for you this time...
Thanks! Not sure what happened, I used the code sample on each.
Answer by Piflik · Oct 25, 2016 at 12:34 AM
You need the using directive in any script, where you want to use the List<T> type.
Don't know if that was a copy&paste error, but the GetOilChangeList() function misses the type parameter. It should be public List<OilChange> GetOilChangeList().
Had to add the List type to the return type to get it to compile. Thanks!
It sort of works now. Now complaining that I can't use new. You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all. Both scripts inherit from monobehavior, so I'm kinda stumped... What do you think Piflik?
while (mReader.Read())
{
OilChangeList.Add(new OilChange(mReader.GetString(0),
mReader.GetString(1),
mReader.GetString(2),
mReader.GetString(3),
mReader.GetString(4),
mReader.GetString(5),
mReader.GetString(6),
mReader.GetString(7),
mReader.GetString(8)));
Debug.Log(mReader.GetString(0) + mReader.GetString(1) + mReader.GetString(2) + mReader.GetString(3) + mReader.GetString(4) + mReader.GetString(5) + mReader.GetString(6) + mReader.GetString(7) + mReader.GetString(8));
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
SQLite Sort and or List Sort 1 Answer
List Set in Editor no longer contains Items after Transplant to new Level (pooler = singleton) 0 Answers
Database input datetime 2 Answers