- Home /
Literal Notation Object Creation in unity?
I am trying to create an object containing information about my character but for whatever reason, whenever I try using literal notation instead of a constructor I get compiler issues. It would save me a lot of time typing and make my code neater if could use literal notation. Here is a small example of the code:
var skills = {
//Basic weapon skills
unarmed: 10,
onehanded: 10,
twohanded: 10,
//Sword specific skills
sword: 10
};
What you have there is Javascript,, not Unityscript. Unityscript is basically C# with Javascript syntax, so you are limited to that functionality.
It looks like you might as well be alright with using a class ins$$anonymous$$d, here's an example:
var skills : Skills;
function Start () {
// Increase sword skill to 20
skills.sword += 10;
}
class Skills {
var unarmed : int = 10;
var onehanded : int = 10;
var twohanded : int = 10;
var sword : int = 10;
}
Ah that helps. What I ended up doing was creating a separate script just to hold the skill variables. In runtime would a class or a separate script run faster?
A script is a class, specifically a class that inherits from $$anonymous$$onoBehaviour.
Yes, scripts written in Unityscript must inherit from $$anonymous$$onoBehaviour. You can create other classes inside the script that don't inherit from $$anonymous$$onoBehaviour, however you will always have that $$anonymous$$onoBehaviour class even if it's empty. You can always attach a script written in Unityscript to a GameObject, no matter what's in it (it may not do anything, but you can still attach it). The same is not true of a C# script.
Your answer
Follow this Question
Related Questions
When generating a procedural voxel mesh, i get something weird. 1 Answer
JS to C# List type problem 1 Answer
How do I fix my platform-moving code? 1 Answer
Far Clipping Range syntax 1 Answer
How to undo cancel Invoke 1 Answer