- Home /
Setting a new runtime class variables gives Null reference
I have a class:
class aPart
{
var partName : String;
var partDescription : String;
var partIcon : Texture2D;
var partIconRect : Rect;
}
public var partList = new Array();
During runtime, i need to make a new part and add it to an array of parts i have:
function storeNewPart(partToStore : String)
{
var newPart : aPart;
newPart.partName = partToStore; //null reference here, why?
newPart.partIcon = Resources.Load(partToStore);
partList.Add(newPart);
ArrangeParts();
}
I am an artist turned scripter, and have been stumped by this for several hours now. I can not find any good solution around this. What is the correct way to create a new part on the fly and add it to my partList?? Thank you for taking a look :)
Comment
Best Answer
Answer by Eric5h5 · Feb 23, 2012 at 07:08 AM
You didn't initialize newPart, you only declared it; it's null (hence the error).
var newPart = new aPart();
Thanks Eric, worked perfectly. I apologize for the silly question, still learning new things daily as I push my scripting abilities. I promise i searched! Apparently I didn't know what I was looking for.
Your answer
