I have recently run into a bizarre issue in code that was previously working fine.
I have a list of a custom class in unity, and I have a system to save it to a Database.
For testing purposes, I run a manual save by setting a flag, like this:
if (manualsavetodb)
{
manualsavetodb = false;
print("manual save to db:"); //player data only for now
saveavatarstatetodb();
saveaistatetodb();
}
So, the above code is in Update() and I click on “manualsavetodb” in the editor, and the above code runs.
This used to work fine, but now, whenever I run it the list which stores my items becomes corrupted. It seems that they all become uninitialised somehow (I get nullreferenceexceptions), but here is the really weird thing, if I change my code to this:
if (manualsavetodb)
{
manualsavetodb = false;
//print("manual save to db:"); //player data only for now
//saveavatarstatetodb();
// saveaistatetodb();
}
This issue STILL occurs! Even though I am not actuall calling any functions!
Is this some kind of odd bug within unity? This code seemed to run find before! Not even the print statement is running!
I don’t really understand what you’re trying to do, but I’m fairly certain that it isn’t a Unity bug, but you’re making a mistake somewhere. Like duplicating game object with this code, another script with the same database code, or something else.
if (manualsavetodb)
{
manualsavetodb = false;
//print("manual save to db:"); //player data only for now
//saveavatarstatetodb();
// saveaistatetodb();
}
After being run, (even though it does nothing) is causing my list of objects to become null references.
I’ve now removed everything from the update loop, so all I am doing is printing out a string value from the list, and I have the above code, nothing else is running, and still the list items become null once I click on a button in the editor…
I tried deleting the library folder twice, still no dice.
The entire script is massive, it wouldn’t help a whole lot.
However, all I am doing is this:
public void aitest()
{
print("AITEST");
for (int i = 0; i < persistentailist.Count; i++)
{
print(i + " " + persistentailist[i].player);
}
}
void Update()
{
aitest();
if (manualsavetodb)
{
manualsavetodb = false;
print("manual save to db:"); //player data only for now
//saveavatarstatetodb();
//saveaistatetodb();
}
}
I suspect the issue is with the custom class I am using in persistentailist. I am creating instances of this class and adding them to a list, and they appear in the unity editor as an object with no name, which seems to work fine, and used to, but maybe I need to create them in a different way?
public List<persistentai> persistentailist = new List<persistentai>();
persistentai aio = new persistentai(aname,aiobj.aiid, aiobj.assignment, false, aiobj.playerworldpos, aiobj.aiworldpos, aiobj.aicontrolobject);
public class persistentai : MonoBehaviour
{
public string player;
public int aiid;
public string assignment;
public bool aiactive;
public List<serveraction> mainaiactiondb;
//public persistentservertest pServer;
public avataractionsavailable aaa;
public serveraction currentaction;
public float timeleft;
public PW_Vector3D playerworldpos;
public PW_Vector3D aiworldpos;
public string aicontrolobject;
public persistentai()
{
player = "";
aiid = -1;
assignment = "";
aiactive = false;
currentaction = new serveraction();
timeleft = -1;
mainaiactiondb = new List<serveraction>();
playerworldpos = new PW_Vector3D(0, 0, 0);
aiworldpos = new PW_Vector3D(0, 0, 0);
aicontrolobject = "";
}
But the weird thing is that this code was perfect for six months, it only started causing issues today. At least I’ll know for the future. Thanks a lot for your help!