Declaring a new Object (JS)

the following code does not work

var myobj =new Object();
myobj.strength = 15;

i get a long error, starting with “MissingFieldException: System.Object.strength”.

I am fairly proficient in my javascript knowledge. this is not the first time something i have typed into the unity editor that would work perfectly in plain javascript has not worked. Often times things have slightly different names, like Math in JS and Mathf in Unityscript.

Am i off my rocker? did i forget what i know about objects? or is this just not mean what i think it means.

any help to point me in the right direction to preforming this correctly is greatly appreciated.

Have a Quishtay™ Day!

1 Answer

1

I believe your first line is creating a Unity Object:

http://docs.unity3d.com/Documentation/ScriptReference/Object.html

Unity objects don’t have a ‘strength’. Assuming ‘Object’ is the name of your script/class, try renaming the class to something other than ‘Object’. Also note that you don’t want to use the ‘new’ operator if your class is derived from Monobehaviour.

how do i not make it a unity object? First off i have no idea what a "class" is, i never learned about them, i am trying to achieve this in unity: http://www.codecademy.com/courses/spencer-sandbox/1/5?curriculum_id=506324b3a7dffd00020bf661 I don't think i want to create a "Unity" Object, i just want an object with variables that i can assign to, so that i can easily organize information. Is that just not possible in unity? Sorry I don't understand this very well could you help me understand

Unity does not use web Javascript; don't use any Javascript tutorials. Unityscript is a custom language. It's quite similar to ActionScript3 however. Most ActionScript3 code (including classes) can be ported to Unityscript either directly or with some minor modifications, as long as it doesn't use Flash-specific APIs of course.

alright thank you, and no, I'm not some idiot who just looks things up on the internet and copy/pastes them into the editor, but I thought that the place where i learned about objects, way back when, would help people understand what i was trying to accomplish. I'll just use multi-dimensional arrays instead (because i actually have used those in UnityScript). Thanks again.

cool! this looks like what i need, thanks a bundle. one last question, can you use methods on classes? thanks

Yes, you can have methods inside classes.