How do I create a class, pref unityscript.

Hello.
I have searched ( and will continue searching after I submit this post ) for the answer but the results so far have just been jibber jabber

How do I create a class then create an instance of that class from another script.

Thanks.

If you are using c# for scriping you create and instanciate normal classes just like in java, except that you are using “:” instead of “extends” to signal inheritance (you see it on top of a new c# script you create).
Unity has some special classes however
If you are creating a new script to attach to a gameobject, use MonoBehaviour as parent class like unity does it with new c# scripts.
If you need the script just for an internal class use ScriptableObject, it has some usefull functions and callbacks and need to be created with the static “Createinstance()” method.

Hmmm, interesting. Not sure what the MonoBehaviour is about but the ScriptableObject looks like it could be the thing.

Is a ScriptableObject like a C# script that contains the class like Character.java above then I can stick this script on an empty game object in the scene then call the class from any other script like in the CharacterManager.java above.

So I could then have a GUI label or whatever that could display the data for an instance of this class such as showing the characters gold.

UnityScript can use classes as well. By default you don’t need to add a class definition to a script and it will internally put all your functions and variables inside a class named like the file which extends from monobehaviour. but you can still explicitly declare one:

class MyScript extends MonoBehaviour
{
}

You don’t have to inherit from MonoBehaviour though and can just use a simple class if you don’t need to attach your script directly to a gameobject.

I am getting this compiler error:

Assets/new-player.js(5,9): BCE0048: Type 'character' does not support slicing

this is new-player.js (I would expect it to show woodsman in the console)

#pragma strict
public var Dave : character= new character("Dave", "Human", "Male", "woodsman", "Wise Master",50, 5, 5);

function Start () {
 print (Dave[3]);
}

this is the class character.js

#pragma strict
public class character
{

		private var character : Array = new Array[8];
		
		public function character (name : String, race : String, gender : String, craft : String, rank : String, age : int, hp : int, gold : int)
		{
			character [0] = name;
			character [1] = race;
			character [2] = gender;
			character [3] = craft;
			character [4] = rank;
			character [5] = age;
			character [6] = hp;
			character [7] = gold;
		}
}

I stopped trying to mess with arrays for the class and just used variables instead and now it works.

I put the class file in a ‘plugins’ folder.

Here is the class file character.js

#pragma strict
public class character
{
		public var name : String;
		public var race : String;
		public var gender : String;
		public var craft : String;
		public var rank : String;
		public var age : int;
		public var hp : int;
		public var gold : int;
		
		public function character (_name : String, _race : String, _gender : String, _craft : String, _rank : String, _age : int, _hp : int, _gold : int)
		{
			name = _name;
			race = _race;
			gender = _gender;
			craft = _craft;
			rank = _rank;
			age = _age;
			hp = _hp;
			gold = _gold;
		}
}

And this bit of code is just a test, it creates 2 characters and prints out a bit of info:

#pragma strict
public var Dave : character= new character("Dave", "Human", "Male", "woodsman", "Wise Master",50, 5, 5);
public var Ian : character= new character("Ian", "Human", "Male", "Tailor", "Apprentice",15, 5, 1);

function Start () {
 print (Ian.name+" Is a "+Ian.race+" "+Ian.craft+" And "+Dave.name+" has "+Dave.gold+" pieces of gold");
}

And the console shows;

Ian Is a Human Tailor And Dave has 5 pieces of gold

So there you go, another achievement :stuck_out_tongue:

This is the tutorial I followed:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes