- Home /
Question by
$$anonymous$$ · Jul 12, 2014 at 11:00 PM ·
javascriptobjectclassitem
Making an item class in Unity.
I'm best with javascript and I've learned to write classes something like this:
function Item(name, description) {
this.name = name;
this.description = description;
this.use = function use() {
//Do something
}
}
:P I believe this is incorrect in Unity, as it doesn't work. How can I do something like this? I aim to do everything I've shown above. (Item class, properties for items, and attaching a function.)
Comment
Answer by Eric5h5 · Jul 12, 2014 at 11:08 PM
You do it the same way as ActionScript3:
class Item {
var name : String;
var description : String;
function Item (name : String, description : String) {
this.name = name;
this.description = description;
}
function Use () {
// do something
}
}
Sweet! Will I be able to call the function by using Item.Use();
or get a property like Item.Name
?
Hm, what am I doing wrong here?
#pragma strict
public class Item {
var name : String;
function Item (name : String) {
this.name = name;
}
function Use () {
Debug.Log(this.name);
}
}
function Start () {
var Box = new Item ("Box");
Box.Use();
}