Hi, so I am kind of new to javascript and to unity. My latest attempt at making an object ended up something like this:
So if I understand correctly, JS objects, unlike in c++, can be initialized using a function that assigns values to this.whatever and can be initialized by doing name = function(). I believe you can call on them by using name.whatever, right?
Well, anyway, here is my object.
function partHullClass(partLoc: Vector3, guiLoc: Rect, number : int){
this.partLoc = partLoc;
this.guiLoc = guiLoc;
this.number = number;
this.isPartUsed = false;
this.onGUI = onGUI();
return this;
}
function onGui(){
if (GUI.Button(Rect(10,70,50,30),"Plus")) {
if (!isPartUsed) {
partHullInstance = Instantiate(jaska, partLoc, transform.rotation);
isPartUsed = true;
partHullInstance.AddComponent("FixedJoint");
partHullInstance.GetComponent("FixedJoint").connectedBody = rigidbody;
partHullInstance.GetComponent("FixedJoint").breakForce = 5;
partHullInstance.GetComponent("FixedJoint").breakTorque = 5;
} else {
Destroy(partHullInstance);
isPartUsed = false;
} }
paikka = new Vector3(-3,-3,0.75);
shipHull = new partHullClass(paikka, Rect(10.0,ekatop,koko,koko), 1);
shipHull.onGui();
Sorry about some of the finnish variable names. Also, on the side, I don’t know why I had to use return.this, but it seemed to stop some errors.
But now, every function after it in the code looks like this when I try to compile it:
function Update () { Expecting (, found 'update'. (BCE0044)
if (Input.GetKey("w")){ (on same line) ; expected, insert semicolon...
rigidbody.AddRelativeForce (0, 5, 0);
}
if (Input.GetKey("a")){
rigidbody.AddTorque (0, 0, 5);
}
if (Input.GetKey("s")){
rigidbody.AddRelativeForce (0, -5, 0);
}
if (Input.GetKey("d")){
rigidbody.AddTorque (0, 0, -5);
} }
Can anyone explain this?
Disclaimer: I know it is probably an obvious mistake, but I am new to JS so have mercy.