How do you add an object variable
Let’s say
myobject = some object like a game object
can’t you just say
myobject.type=“car”
or do you have to do something else first?
I tried this and it didn’t work.
Thanks
Dan
How do you add an object variable
Let’s say
myobject = some object like a game object
can’t you just say
myobject.type=“car”
or do you have to do something else first?
I tried this and it didn’t work.
Thanks
Dan
This reply is about Javascript in general – Unity’s implementation may handle things differently. As to how you do what you are asking, it depends on if you are wanting to add a property or method to just that instance of an object or if you want custom properties and methods to be available to that type of object in a general sense.
edit: I started putting together a little example, but it seems Unity’s implementation of JS handles objects all kinds of wrong… maybe somebody with more Unity-specific knowledge will be able to offer up how to do it the Unity way.
FYI - Here is how I’ve always done that… you have been able to do this in Javascript since 1.1. It doesn’t seem to work in Unity, however. To test them quickly, I ran them in a web browser, which is why “alert” is getting called… just as a fast feedback loop.
// example one
function mood(){ }
var myMood = new mood();
var yourMood = new mood();
myMood.today = "happy";
// this should alert "happy"
alert(myMood.today);
// but this will alert 'undefined', because 'today' is only part of myMood
alert(yourMood.today);
So, that example added a property just to that instance.
// example two
function mood(){ }
mood.prototype.today="normal";
var myMood = new mood();
var yourMood = new mood();
myMood.today = "happy";
// this should alert "happy"
alert(myMood.today);
// and since 'today' is part of the mood prototype, this should say 'normal'
alert(yourMood.today);
That example added a property to the prototype, so it is there for every instance.
// example three
function mood(){ }
mood.prototype.today = "normal";
function saymood(){
alert(this.today);
}
mood.prototype.saymood=saymood;
var myMood = new mood();
var yourMood = new mood();
myMood.today = "happy";
// with a method to call, we can just do the following:
myMood.saymood();
yourMood.saymood();
And that example added a method and a property to the prototype and called the method on two instances.
A game object is type GameObject. “var myobject : GameObject;” and then assign the object in the inspector. Or get a reference to it another way…reading the scripting overview in the docs is a good idea.
–Eric
I use javascript all the time, but usually when you have a predifined object, not necessarily a function you can just dot properties to it.
Car = new Object();
Car.wheelnum=4;
etc…
But in Unity this does not work if it’s a game object?
I’m wondering if I have to tell it what type of variable it is
like in actionscript this would be
var Car. wheelnum:int = 4;
Dan
I think ‘type’ was just a bad example. Let’s say ‘vehicleType=“car”’ is what was being set.
I basically want to add new properties to a gameObject. I just did a test and my example above works.
But not if it’s a gameObject?
can’t you just add properties to a game object – like anything you want?
Dan
Unity’s JavaScript is only JS in syntax, not in object handling. You can’t create new fields for an object at runtime. Any .js scripts you create in Unity are implicitly classes derived from MonoBehaviour (the filename determines the class name). Any variable declarations outside of functions are considered public instance variables for that class.
I see this is as more of an intellectual curiosity, instead of something I’d want to do in production, but because Unity’s JavaScript is based on Boo it is possible to support ECMAScript behavior somewhat:
http://www.unifycommunity.com/wiki/index.php?title=ExpandoObject
I basically want to add new properties to a gameObject. I just did a test and my example above works.
But not if it’s a gameObject?can’t you just add properties to a game object – like anything you want?
Dan
You probably want to add new properties to a component on a GameObject (ie your script). You simply do this by declaring the variable outside of a function.
IE:
var myPublicVariable:int = 3;
function Update()
{
// do something useful here
}
Note that you don’t need to explicitly define the variable’s type, but it tends to be a good habit…
The only difference is I want to add these properties from my main script
worldsetup.js not from an individual gameObject script.
There should be some simple way to add your own properties to a standard object no matter what it is. Of course this is compiled so I’m thinking I would have to tell the compiler what kind of variable it is.
I can’t find any info on dotting properties to anything…
Thanks for all the lightning quick responses guys…
Dan
Actually I didn’t see your first post…I’m reading it now…
That makes me sad… Sorry that there wasn’t an easy answer in there for you. I was looking forward to:
function Vehicle() {
this.name = "";
this.wheels = 0;
}
function Car(){
this.wheels = 4;
this.passengers = [];
}
Car.prototype=new Vehicle();
var myCar = new Car();
myCar.name = "Awesome Car";
I had the feeling as soon as I started realizing that it wasn’t prototype based that it wasn’t really JavaScript… just something pretending to look like it.
Actually I had the Unity guys develop the eval() function for me which is not working apparently. And part of the eval function was to add the expandoObjects to javascript at least for the eval function.
the reason was so that you could access dotted properties in an eval statement.
This was done internally and not something from the unify site.
I’m not sure what is going on now since the function isn’t working anyways.
Hopefully they will fix it soon.
Maybe you could add them from the eval command?
Well I tried it and it doesn’t work either.
Dan
The only difference is I want to add these properties from my main script
worldsetup.js not from an individual gameObject script.There should be some simple way to add your own properties to a standard object no matter what it is. Of course this is compiled so I’m thinking I would have to tell the compiler what kind of variable it is.
Let’s attack this from another angle. What are you trying to do (not technically, but architecturally)? Have multiple scripts access the same variables? Keep track of user-inputted values? What do you need the expando functionality for?
On JS: ActionScript abandoned the prototype style of coding with AS2. It’s still in use with browser JS coding, but other ECMAScript implementations moved on. Your car/vehicle example is better served by true inheritance anyway:
class Vehicle extends System.Object {
var name = "";
var wheels = 0;
}
class Car extends Vehicle{
var wheels = 4;
var passengers:Array;
}
var myCar = new Car();
myCar.name = "Awesome Car";
On JS: ActionScript abandoned the prototype style of coding with AS2. It’s still in use with browser JS coding, but other ECMAScript implementations moved on. Your car/vehicle example is better served by true inheritance anyway.
Oh, no… I would prefer a class-based language to a prototype-based language any day. It bothers me a little bit to call it “Javascript”, because prototypes are sort of a defining characteristic of that language… versus calling it “UnityScript – a scripting language with javascript-like syntax” or something like that. So, I was being a little facetious with regards to disappointment – but not to my surprise: You read “javascript” and expect the language to behave like javascript – warts and all. I think that assumption is part of what lead whimsica to expect to be able to add a property to gameObject. I know that I had always assumed objects would be handled the same way until discovering that it wasn’t true just now.
Ah, yeah. That issue has been raised before, and it makes a lot of sense. A lot of people look for “JavaScript” tutorials and end up reading a bunch of unrelated and confusing information.
The big benefit to calling it “UnityScript” would be much easier Googling. Any results for “UnityScript tutorials” would be exactly on topic.
I just want the new properties to travel around with the object. That is the main goal. There is already one solution of putting the properties in the gameObject script ahead of time. But my system creates the object on the fly from the main script rather than having a lot of little scripts all over the place with separate variables. I don’t see an easy way to do this yet.
How about this?
myobject= new Object();
myobject.gameObject=gameObject;
myobject.acceleration=Vector3(0,0,0);
Dan
Why not use a Hashtable?
var myHash = new Hashtable();
myHash["gameObject"] = gameObject;
myHash["acceleration"] = Vector3(0,0,0);
As far as I’m informed, Unity’s JavaScript is developed by De Oliveira, who is the maker of Boo. I’ve read that Unity’s JS is basically an adaption of Boo to a JavaScript syntax.
I’ve been going between JS, Boo and C# and comparing those languages, especially learning boo helped to understand Unity’s JavaScript better.
Also, scripting in Unity is based on Mono and .net’s CLI. A browser-like JavaScript implementation would probably be very hard or even impossible. Unity’s JavaScript might be better compared to Microsoft’s JScript.Net.
Thanks for the hashtable idea.
It appears that new Object() is meaningless in UnityScript? I can’t attach a property to an object even if I create it myself.
Apparently
myobject=new Object(); does work
myobject.property1 = 1; //does not work.
So it seems that objects are useless here.
Is a hash table the best solution? the only one that will work.
I like it except the quotes – which will get messy later in eval statements.
If they fix eval()…
If I am going to use a property often. Is it possible to permanently write a property into the GameObject itself, or the gameObjects’ rigidbody maybe?
I would like lots of physical properties of my own like
temperature
myacceleration
myforce
buoyancy
density
etc…
Now I am beginning to wonder what else is missing from the javascript that I will run into. Associative Arrays? Objects are not available?
How about using a class? Or just access the variables on the GameObject’s script? I know you already mentioned that, but objects usually have their own scripts so you might as well take advantage of that. Depends on what you’re doing of course.
–Eric