- Home /
list.Add not accepting new class as instance?
Ok so I am trying to create a save load feature. Still. I am trying to save a list of classes that define the name, position, and rotation of the components. But for some reason when I run it in unity it throws up flags claiming that I am not using an instance of an object.
the function in question goes as follows:
function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion)
{
var NewPartData = new PartData();
NewPartData.PartName = newName;
NewPartData.PartPos = pos;
NewPartData.PartRot = rot;
ShipData.Add(NewPartData);
}
As you can see I declare the NewPartData as a new PartData object then try to put it in the list. That's how you instance a non-monobehavior class right? Why is this not working?
Thanks in advance for any help provided
Full Code:
import System.Collections.Generic;
import System.Xml;
import System.IO;
@XmlRoot("ShipSave")
public class ShipSave
{
@XmlArray("ShipData")
@XmlArrayItem("Part")
public var ShipData : List.<PartData>;
function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion)
{
var NewPartData = new PartData();
NewPartData.PartName = newName;
NewPartData.PartPos = pos;
NewPartData.PartRot = rot;
ShipData.Add(NewPartData);
}
function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion, group : WeaponGroup)
{
var NewPartData = new PartData();
NewPartData.PartName = newName;
NewPartData.PartPos = pos;
NewPartData.PartRot = rot;
NewPartData.Group = group;
ShipData.Add(NewPartData);
}
public function Save(path : String)
{
//TODO if file already exists, delete it first then write new one
var serializer : XmlSerializer = new XmlSerializer(ShipSave);
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
public static function Load(path : String):ShipSave
{
var serializer : XmlSerializer = new XmlSerializer(ShipSave);
var stream : Stream = new FileStream(path, FileMode.Open);
var result : ShipSave = serializer.Deserialize(stream) as ShipSave;
stream.Close();
return result;
}
//Loads the xml directly from the given string. Useful in combination with www.text.
public static function LoadFromText(text : String):ShipSave
{
var serializer : XmlSerializer = new XmlSerializer(ShipSave);
return serializer.Deserialize(new StringReader(text)) as ShipSave;
}
}
public class PartData
{
var PartName : String;
var PartPos : Vector3;
var PartRot : Quaternion;
var Group : WeaponGroup;
}
Answer by CiberX15 · Feb 08, 2014 at 07:44 PM
I am an idiot.
Literally within seconds of posting this I realized what my problem was.
I forgot to instance the list. Instead of:
public var ShipData : List.<PartData>;
I should have had this:
public var ShipData : List.<PartData> = new List.<PartData>();
With that fixed everything seems to be working, yay!
Your answer
Follow this Question
Related Questions
[solved] How to save instance of the texture? 2 Answers
Set Dirty on class instance? 0 Answers
PlayerPrefs HighScore problems, it doesn't work. 4 Answers
PlayerPrefs.GetInt won't load when awake. 0 Answers
Getting any type in Unity Inspector 1 Answer