Model Classes...?

Hey, I’m working on a project in which i need to use some of model classes (javascript), but when ever I create a variable using this object, it says that its a null reference.

Here’s my code.

var newSet : CommandSet;

When every I try to do anything with newSet it says its a null reference.

the CommandSet class looks like this.

class CommandSet {
	var name : String;
}

(there is a bit more to the class, but essentially that is what it is). I am assuming it thinks that this is a complete script, and therefore needs to be added to a game object as a component. How do I tell it that this is a model class, and not a interface class?

Try creating it like this:

var newSet : CommandSet = new CommandSet ();

Tried that, no luck.

Ok think I found what I need to do, I think I need to extend ScriptableObject, however, I still get the null reference error, I assume I need to use

ScriptableObject.CreateInstance(“CommandSet”);

However, when I do that it returns

Instance of ScriptableObject couldn’t be created because there is no script with that name.
UnityEngine.ScriptableObject:CreateInstance(String)
UnityEngine.ScriptableObject:CreateInstance(String)
Menu:OnGUI() (at Assets/Scripts/Menu/Menu.js:250)

[/Users/build/builds/unity-branches-2.5.x/unity-2.5.x/Projects/…/Runtime/Mono/MonoExportUtility.cpp line 611]

even though there is a class called CommandSet that extends ScriptableObject.

Ok, making a individual script for each class ( >.< ) I was able to get rid of the warnings, however I wind up getting the null reference again.

null exception means that you didn’t create a valid class instance.

If you didn’t extend the class from anything specific, its a monobehaviour extend in which case they are initialized and returned through .AddComponent called on a gameobject.
Do not use new with these.

if you extended from System.Object or UnityeEngine.Object or other .NET classes, you need to create the instance with new.

you might want to call that new in the Awake function potentially instead of the “in place” instantiation suggested above. That gets you instantiation and default setup into a single clean place.

so if we are in script “somescript.js” (-> which means this is class somescript extending from MonoBehaviour) that would look like:

var newSet : CommandSet;

function Awake(){
  newSet = new CommandSet ();
}

and assumes that there is a file called CommandSet with a class CommandSet that expands from anything else than ScriptableObject or a descendant of it (monobehaviour extends from it too)

I have tried that, but I still get the null reference. I have also tried that while having CommandSet extend System.Object, and ScriptableObject. Each time I get the same results.

Are you sure your NullReference error is referring to the newSet variable itself? I.e. that it is reporting newSet to be null, not that newSet.someProperty is null?

Looking at it again, it does look like its pointing to the name var as the null reference.

please post the different related source files (own code blocks each) as well as the exact error from the console so we can try to nail it.

from the theoretical point of view, above covers all cases and its basically impossible to get it to fail unless you have other errors.

function in CommandSet

function NewCommandSet() {
		cName = "New Command Set"; // <- Where the error comes from.
		var newLevel : AdminLevel;
		newLevel.NewAdminLevel();
		var a : Array = new Array(newLevel);
		adminLevels = a.ToBuiltin(AdminLevel);
}

newSet code

var newSet : CommandSet;
newSet = CommandSet.CreateInstance("CommandSet");
newSet.NewCommandSet(); // <- If I remove the cName line, it says the error comes from here.

error message

NullReferenceException: Object reference not set to an instance of an object
CommandSet.NewCommandSet ()   (at Assets/Scripts/Frameworks/Command Set Stuff/CommandSet.js:7)
Menu.OnGUI ()   (at Assets/Scripts/Menu/Menu.js:252)

No matter how I initialize the class, what the class is extending, I pretty much get those results ( I used to get 1 more error, but ScriptableObject fixed that ).

the problem is that you don’t initialize it.

var newLevel; // now newLevel = null. Dooohh!
null.SomeMethod() // I'm bullocks, please save me ;)

right would be

newLevel = new AdminLevel();// horray now newLevel != null
newLevel.SomeMethod(); // Horray thats correct

I guess you are still pretty much a beginner in programming and understanding error outputs. Don’t worry, over the time you will understand the errors the moment you see the line in case of such trivials.

I have tried that as well, get the exact same error.

then admin level again expands from scriptable object or higher and thus new can’t be used.

cName is a var declared outside the function, thats right?
because each js script is a class itself, extended from MonoBehaviour.

there are no global variables that are not part of an object

cName is declared outside, so is adminLevels. AdminLevel and CommandSet extends ScriptableObject, I have also tried extending from System.Object (both times using as many methods of initialization as I could think of).