Is the error I get whenever I try to edit the values of a class I made called InputBasic.
Last weekend I started working with classes, and had BaseInput working. Take a break for several days and now this is throwing an error.
I must have done something - - but I can’t figure what’s causing it.
The code
function GetBaseInput() : InputBasic{
//This gets the basic input.
//This function returns what movement keys are held down.gg
var w : InputBasic;
if (Input.GetButton("move_forward")){
// Debug.Log("dah!");
w.wasd = Vector3(0, 0 , 1);
}
if (Input.GetButton("move_back")){
w.wasd += Vector3(0, 0 , -1);
}
if (Input.GetButton("move_left")){
w.wasd += Vector3(-1, 0 , 0);
}
if (Input.GetButton("move_right")){
w.wasd += Vector3(1, 0 , 0);
}
if (Input.GetButton("key_slow")){
w.slowkey = true;
}
if (Input.GetButton("key_run")){
w.slowkey = false;
w.runkey = true;
}
//return w;
}
//(MovementClass.js, located in StandardAssets/Scripts/MyClasses)
class InputBasic{
var wasd : Vector3 = Vector3(0,0,0);
var runkey : boolean = false;
var slowkey : boolean = false;
}
In the future, include the line number, and indicate the line of code that’s throwing the error.
But this is your problem:
var wasd : Vector3 = Vector3(0,0,0);
var wasd : Vector3 = new Vector3(0,0,0);
//or
var wasd : Vector3 = Vector3.zero;
You have to instantiate your objects, otherwise you aren’t actually creating anything. The new keyword solves your error. Use this when creating instances.
Vector3.zero is a static variable that points to a vector with a value of 0,0,0. Static methods are stored in the class itself, and not actually within an instance object, so they are accessible in any namespace where the class is defined.
var w : InputBasic;
You never assign a value to “w”. So when you try to access “.wasd” on it, it throws that error (trying to access “wasd” on a null/non-assigned variable)
EDIT: TerXIII, I thought in UnityScript, you can create structs without the “new” keyword; I think that code may be valid.
EDITx2: Be sure to restore your “return w;” line otherwise the method is no good.
Good point there Fizix, I’m coming from engines (And my own engine I wrote in Java) that uses classes for Vectors… seeing as Structs don’t exist in Java. Very good point. (Plus, I seem to be part of the vast minority of users who actually uses C#.)
I’m with you on that one. I’m not a fan whatsoever of UnityScript’s lenient syntax regarding the creation of structs (though this may be stemming from ECMAScript)
Agreed, with an API, anything but consistency is inadequate.
With unityscript I don’t believe you have to use new to declare a var. Nor do you have to set functions/classes as public.
I do need to move to C#, since that seems the far better supported (2d arrays) and the references now have c# documentation. But than there’s the learning period + the feeling that I need to port what I’ve scripted over to C#.
Back to the problem : I get the error message whenever trying to access v (or any of the classes I’ve made). Doing a Debug.Log(v) returns void.
How would I go about fixing this? I’ve done google unity searches but haven’t found anything useful yet.
2D arrays are quite worthless, actually. They utilize more memory, are slower to access, harder to work with, and serve absolutely no purpose. A single dimensional array can be accessed as a two-dimensional array by width*y+x, and x = index%width; y=floor(index/width)
The larger the array, the greater the impact this makes, though, it’s really more like counting fractions of pennies when it comes to performance and memory.
Also, Windex, you never need to declare new with a primitive data type. They are primitives, and not objects. Structs are similar to classes, except they are merely a memory mapping technique that is the basis upon which OO programming was built.
C# has some “negatives”, such as having to learn static typing, declaring callbacks, outputs, references, etc. But of course, Stock Javascript can make these processes more complex in their attempt to make them easier to work with…
I dislike Unityscript merely because I am more comfortable with mainstream languages, and as such, it’s merely preferential more than anything else. There is also a wider availability of C# programming material across the web.
As for your error, you might have to suppply us with a better example of what you’ve done. The error is probably because you still aren’t initializing instances properly, or are mistaking classes with objects.
Yeah, I agree with TerXIII, I don’t think you’re initializing your custom classes. Primitive types (int, float, Vector3) do not need to be explicitly initialized. ALL custom classes must be initialized using the “new” keyword:
var i : int; //valid
Debug.Log(i.ToString());//outputs "0"
var v : Vector3; //valid
Debug.Log(v.ToString()); //outputs 0,0,0
var x : MyCustomClass; //valid, but it is a null value
Debug.Log(x); //outputs null (or nothing)
Debug.Log(x.ToString()); //throws an "Object reference not set to an instance of an object" error because you call "ToString" on a null object
var y : MyCustomClass = new MyCustomClass(); //valid, created a new instance of MyCustomClass
Debug.Log(y); //outputs some non-null value
Debug.Log(y.ToString()); //outputs whatever your ToString method implemented
var z : MyCustomClass = MyCustomClass(); //invalid syntax, will not compile... SHOULD not compile >.>
Alright, cut discussion. Continue it in another thread. I’m going to completely restate my problem.
I decide to make a class. I am using UnityScript. Unity 3.0. Uniscite.
Standard Assets/Scripts/MyClasses/Movement.js
I am using this class to store the player’s input.
class InputBasic {
var wasd : Vector3 ;
var runkey : boolean ;
var slowkey : boolean ;
}
My Tests/Scripts/Movement/NewMovement.js
(function condensed. Function is called every update)
function GetBaseInput() : InputBasic{
var v : InputBasic;
Debug.Log(v); //CAUSES ERROR
if (Input.GetButton("move_forward")){
v.wasd += Vector3(0, 0 , 1); //CAUSES ERROR
}
if (Input.GetButton("key_slow")){
v.slowkey = true; //CAUSES ERROR
}
return v;
}
When the bottom code is ran v will not initialize. It will throw the error whenever I try to access it.
What must I do to resolve this error?
“Alright, cut discussion. Continue it in another thread.”
I don’t feel this was in good taste, windex. Your question was answered indirectly, but now that you have better explained yourself, I know where you are missing a concept.
You want InputBasic to be a singleton class, utilizing static methods. Since this is the case, you have to declare the variables as static members of the class, and then access the class within the namespace context.
class Derp
{
static herp : int;
}
Derp.herp = 0;
However, if you are attempting to use an object that has been initialized elsewhere, review our earlier posts, as we figured that was what you were attempting to do.
Static types can be a bit confusing at first, and proper usage of them is not often seen in the amateur programming world, so it’s not surprising this issue came up. It’s a pretty simple fix, though.
I meant towards the C# / JS discussion, it was going off topic. Apologies in taking part of it and giving off the wrong idea.
Gave the variables ‘static’ and the function is now working fine. Thanks
No problem, sorry if I came off as harsh, as I didn’t interpret it as being implicitly off topic, as after my C#/JS diatribe (which was all moving toward an end goal) I threw in this tiny nugget of information.
"As for your error, you might have to suppply us with a better example of what you’ve done. The error is probably because you still aren’t initializing instances properly, or are mistaking classes with objects. "
Which was the real point of my C#/JS discussion. Sorry if I got a little long-winded in the process, I tend to do that when I’m moving toward a point in the process of trying to teach a lesson, thereby giving a solution.
I’m glad it worked out for you.