Hey everyone. I have a question about classes in JavaScript. I know how to use them in the code but I want to know how often you guys use them in your games as well as when they are most helpful and when they are just overkill. I had considered making a game that has several reusable game objects with very extensive code with a lot of parameters and I thought that creating a class for these objects would be the best thing to do, but I'm not sure. Also, if I did create a class in one JavaScript file, could it be easily accessed from another script? Any recommendations, advice, or links to helpful material is welcomed. Thanks.
I'm curious what your definition of 'class' is - it's literally impossible to do anything in Unity without using a 'class' - are you talking about a subclass of object, or custom subclasses of MonoBehaviour in general?
An example of what I'm talking about is this: Code (csharp): class className { //Define lots of variables here } I guess it would be a subclass of MonoBehavior.
If your talking about custom classes they don't have to be a subclass of monoBehavior. You can simply make a Datatype.. Ex: Code (csharp): [System.Serializable] public class PlayerData() { public string playerName; public int playerCurrency; public float playerDamage; //Etc.. } How you could use this is simple just adding a new one of that type in your script.. Code (csharp): PlayerData myPlayer = new PlayerData(); This is obviously c# though and this would be created in a Monobehavior class that would be added to a gameobject. From there you could do whatever you wanted with the data. ** [System.Serializable] makes the Custom class viewable in the inspector.