What does a Class ‘do’?
Is the script itself the ‘Class’? If not provide example, which is class and which is script?
What is the purpose of the class?
I have seen the Class tutorial 3 times and I still dont get it…
What does a Class ‘do’?
Is the script itself the ‘Class’? If not provide example, which is class and which is script?
What is the purpose of the class?
I have seen the Class tutorial 3 times and I still dont get it…
When you declare a class, you write a blueprint of an object.
You have to see it that way, the class is the blueprint the computer will use to build an object.
Imagine you want a car. You can’t just snap your finger and bam… A car is made. You have to describe it first. Color, size, wheels, engine, door, etc… and most importantly, how it behave. If I press the pedals, what does it do?
So once you got that blueprint, you would give it to a engineer and he would build it for you. In this context, you give that blueprint to the computer, and it gives you a car - an instance.
So this is a blueprint - a class;
public class Car
{
public Color color;
}
And this creates the car - an instance;
Car myCar = new Car();
In the case of Unity, the editor sees a file - a .cs, .js or .boo file - as being a “script”. This is a somewhat accepted non-sense, because in the end, the file contains a class. In other editor, a “script” would be a self-contain file that contains code interpreted at runtime. However, in Unity, those “scripts” are compiled and run as normal managed code.
On top, a file may contains multiple class, and a class can also contain other class.
So to keep it simple, let’s just assume that a “script” for Unity is a file that contains a MonoBehaviour.
Excellent starter description @LightStriker_1
@DarkEcho , once the Car object has been created by instantiating the “new Car()”, you will be able to do things with it. Here is a Car class in further detail…
class Car {
public var numWheels : int = 4; // externally accessible
private var bodyColor : Color; // available only to this instance of this object
// Constructor, when this car is instantiated, this is "Main", use this to set up some stuff.
public function Car ()
{
SayHello("Hello world, I am a car!");
// note: when you get a handle on these classes, look at coroutines
}
// this method is publicly accessible, dynamically change its bodyColor variable
public function SetColor (c : Color)
{
bodyColor = c;
}
// this method is only available to this object
private function SayHello (s : String)
{
Debug.Log(s);
}
}
Note the public and private member variables and methods (functions). After the car has been created, you can adjust things about it.
var myCar : Car = new Car(); // notice different syntax in JS
myCar.numWheels = 3; // now a 3-wheeler!
myCar.SetColor(myRGBcolor); // assign a body color
myCar.SayHello("Trying to say Hello!"); // this one will bomb because it is a private method!!!
If you intend to use C#, here is a great “simplified” write-up on classes:
For JS, read this:
But keep in mind UnityScript is not really JavaScript but this example is still relevant.
So the “Script” is the file itself and the file contains the “Class” of how to perform Functions/Variables or make a car? Correct?
And the purpose of the class is just to make a car, I.e the Class is the engineer to make this car? Correct?
Class = blueprint
You = designer who draw a blueprint
Computer = engineer who construct an object using a blueprint
Script = files containing classes
I would disagree and strongly suggest disregarding anything to do with ECMAScript (web JavaScript).
Oh so Class is just a set of instrcutions and thats it.
Also I understand a script can have more than 1 Class.
Instructions (methods) and properties (fields). Car can roll, but a car also have a color.
True, I just couldn’t find anything similar for UnityScript. Just looked for something super simple so OP could get his head around the subject matter: properties, methods, inheritance. I will find something better. Maybe you know of a good example?
This is a much more accurate description of the relationship.