Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I have a ton of experience with JavaScript (its what I do for a living) but I am new to unity. I have built a game engine in javascript but now want to move it over to unity. I have been trying to implement my player class but am running into this error.

I have a getter function for my level_up stats. and inside of my level_up function I call that getter function and store it now I know this would work in normal javascript but its not in unity and I have having a heck of a time googling any information on objects in unityscript. Here is the relevant code:

function level_getter(){
    return {base_level_exp : float , lvl_exp_modifier : float , current_level : int};
}

function level_up(){
    var level_object = new Object();
    level_object = level_getter();

    level_object.current_level += 1;
    level_object.base_level_exp = level_object.base_level_exp * level_object.lvl_exp_modifier;

    level_setter(level_object.base_level_exp, level_object.lvl_exp_modifier, level_object.current_level);

}

I guess my first question is, is it even valid to return an object like that in unityscript?(in the level_getter() function). If its not valid what do people suggest I do instead. Set a getter function for each var? My second is how do I declare level_object so that it can accept the return if it is valid, and so that I can access the members of the object.

I have been looking around at tutorials and google for several hours now and have found nothing to clear up my confusion over objects in unityscript. So any help would be appreciated.

share|improve this question

2 Answers 2

Be careful, Unityscript is not Javascript. Here an example on how to solve your Problem:

Your level object:

class PlayerLevel {
    public var base_level_exp:float;
    public var lvl_exp_modifier:float;
    public var current_level:int;
}

Your levelup thing:

function level_up() {
    private var level_object:PlayerLevel = new PlayerLevel();


    level_object.current_level += 1;
    level_object.base_level_exp = level_object.base_level_exp * level_object.lvl_exp_modifier;

    level_setter(level_object);
}

This is just an example, it is untested and should just illustrate how to create custom objects. I hope I could help you to see some differences between Javascript and Unityscript.

You might want to read the following article: Head First into Unity with UnityScript

share|improve this answer

I'm not an expert in unityscript but I'm relatively familiar so i'll make an attempt. Im also at work and don't have access to unity so any code here may not be 100% =-)

While I'm not totally fluent in JS, it looks like you are trying to use something like JSON to pass data and while unityscript syntax is close to JS, I don't believe it works the same way.

I think what you will want to do is to create an object of a type "PlayerStats" or soemthing like that to keep track of the player info and instead of var level_object = new Object(); you would do var level_object : PlayerStats = FindObjectOfType(PlayerStats);

That should give you access to all of the member vars in PlayerStats and allow you to mess with them.

Since you are new to unity, might i suggest the tutorial sets here: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial - They are pretty extensive and show a lot of how to pass data between objects.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.