Javascript Object created...now what?

`
var DamageRes=0;
var ForceRes=0;

function Armor()
{
this.DamageRes=0;
this.ForceRes=0;
}`

As you can see I wrote code to create a object in java script. I guess where I’m confused is… what do I do with it? By attaching this script to a game object, what occurs exactly? Do I need to create a java script object within this script for the game object to have the properties of armor?

2 Answers

2

Technically, by writing this, you did not create an object. You created a class that derives from UnityEngine.MonoBehaviour-class that derives from UnityEngine.Behaviour-class that derives from UnityEngine.Component-class that derives from UnityEngine.Object-class that derives from System.Object-class. (So it’s an Object because it’s derived from Object-class, but it’s not an Object in the ObjectOrientedProgrammingTheory sense)

have a look at Unity’sRuntime-Class-Hierarchy (unfortunately System.Object is not noted in this list. only UnityEngine.)

Only when you attach it to a GameObject you are instantiating it and thereby making it an Object in the oop sense. But note: Components are sort of ‘crippled’ Objects as they can not exist on their own but only when attached to a GO.

So when you attach it, it’s being instantiated and a ‘subObject’ for the GameObject is created.

The GameObject now has - via its ‘subObject’ - the variables ‘DamageRes’ and ‘ForceRes’ (you don’t have to do anything else for the GameObject to ‘have the properties’).

But it’s not doing anything with it. The function is never called.

As aldonaletto explains, it needs to be set off by built-in functions that are themselves set off by the UnityEngine at given times.

Additional to the ScriptReference, you can have a read on functions that can set off other functions

So you can not create a ‘javascript object within this script’ in the oop sense. But I’m guessing you are asking if you have to create ANOTHER something in this script. Obviously you can not create an additional ‘javascript object within this script’ either.

You could create another class in this script, but that is not particularly good conduct and for you level of knowledge completely irrelevant.

As aldonaletto says, you should do some more tutorials first. start here [it has a bit of a super-noob touch to it, but the info is valid. I’m quite certain you can still learn from it; esp. to get a starting-point on how read and understand the Scripting-Reference is an invaluable], then go to the endless list of stuff and when you have 2-3 of the big tutorials down and still got knots in your head, then come back…

additional Note: you should start variable-names with lower case and function-names with upper case.

Edit: don’t forget to mark an answer ‘accepted’.

Edit2: Please don’t invent new tags (java script) when there are perfect substitutes (javascript)! >.<

Greetz, Ky.

Nothing will happen with this code - it only defines two variables and a function.

In Unity there are some predefined routines - Awake, Start, Update and others - that are called (if existing in your script) at specific moments: Awake is called once after instantiation, Start is called once after Awake, Update is called before each frame is rendered. To do something with your script, you must attach it to some object and use at least one of the predefined routines - usually Update. Take a look at the Unity Script Reference - they explain these things and all you need to know about scripting in Unity. They supply good examples too, and you can make useful scripts with them.