- Home /
Other
How -exactly- do classes and structs work in Unity?
Okay, so I read somewhere else on this site that when you attach a script to an object, as each script is a class, you are actually instantiating an object of that class. Is that correct?
So say I write a script like
public class Test
{
public int x;
public int y;
}
If I add this script to a gameobject, does that instantiate an object of that class? The variables x and y would then show up in the Inspector, right? Is there any name for that object I can use to refer to it, or do I have to use GetComponent?
I've also heard that Unity automatically "includes" every script into every other script. Does that mean I could for example write one script which has several generally usable functions in it, and could then use those functions in other scripts? Say I have a class Math with a function Add, could I simply use Math.Add() in any other script? Do I have to put that script on a GameObject first?
What if I was for instance to make a class like
public class Structure
{
struct Test
{
int x;
int y;
};
}
If I put that script on a GameObject, will the struct Test be automatically instantiated, so I could use GetComponent on the script and then use Structure.Test.x? If not, how would I go about doing something like that?
Just some general questions I have.
Unity Answers deals with single, specific technical issues concerning Unity. Your question has multiple technical issues, and largely has nothing to do with Unity. It deals with the ins and outs of C#. I highly encourage you to pickup any of the many beginning C# books and work through the chapters on classes. Here are some points to help direct your reading:
Classes derived from $$anonymous$$onoBehaviour are created by attaching the script to a game object. They must have a game object to exist. They are called components.
Instances of classes *not*derived from $$anonymous$$onoBehaviour must be created using the 'new' operator.
$$anonymous$$ost classes have instance data. For example, if you have a one AI script attached to multiple game objects, then each one may have a 'health' variable that is specific to that instance.
A function in a class that has the 'static' keyword in front of the function declaration can be called in the way you list above with '$$anonymous$$ath.Add()'. But these functions cannot reference instance data in the class. The functions in Unity's $$anonymous$$athf class are all static and therefore can be called like '$$anonymous$$athf.Sin()'. For other $$anonymous$$onoBehaviour-derived classes, you will need to get a reference to the instance of the object to change (Often using GetComponent()).
The biggest difference between classes and structs is that structs are by value, and classes are by reference. That is when you assign a struct to a new variable you get a copy of that variable. When you assign something that is an instance of a class, you assign a reference (and therefore the old and new variables are pointing at the same object.