What does it mean and what is it called? Does it make a copy of all the variables and its values from the class BuildManager at the time it is executed and store it all in buildManager?
When you use the word ‘instance’ that’s generally a tell for some kind of a Singleton pattern going on, a possibly longer-lived class instance that exists only once (hence “singleton”) and unspecified areas of the code may need access to, such as a GameManager or other type of global mechanism.
C# classes are passed by reference, so if that BuildManager is a class, it is just a reference, not a copy of everything.
This is part of something called a singleton. No copies of data are being made here.
Classes have normal methods and variables that you often use, which are part of the instance - each copy of that class has its own copy of that variable. These are instance members. There’s another option, where the class itself has a single copy of the variable, and this is shared by every copy of the class. These are called static members.
Many classes are composed almost entirely of instance members. Some classes (such as Mathf) are static classes, composed entirely of static methods/members - you never declare or use a Mathf object, you just use the utility functions freely from that class, because the methods are a part of the class itself.
One natural extension of this is called a singleton pattern, which is where there is one and only one copy of the class, but you have a static reference to that copy, so that it’s available anywhere. There’s a number of ways to implement this pattern with varying levels of enforcement/reliability/protection, but here’s a really simple one (that would match the code sample you posted above):
public class BuildManager {
public static BuildManager instance;
void Awake() {
instance = this;
}
}
“instance”, in this case, is a static member that refers to the singular object. This is particularly useful in Unity because it allows BuildManager to use things like Unity’s Update loop and allows you to drag-and-drop references to stuff into its inspector (which is not possible with a purely static class like Mathf.)
So, in your code, buildManager will just be an additional pointer to this singular instance (or singleton); nothing is copied.
That doesn’t have an inherent meaning in C#, but it’s a common way of implementing the singleton pattern (especially in Unity).
Probably there’s a class that looks something like this:
public class BuildManager
{
public static BuildManager instance;
// More code...
}
When you declare a variable static, that means it’s associated with the class itself rather than any specific copy of the class. For example,
MyClass myVar = new MyClass();
MyClass otherVar = new MyClass();
myVar.x = 7; // sets an instance (i.e. non-static) variable
otherVar.x = 5; // myVar.x is still 7; otherVar.x is separate
MyClass.someStaticVariable = 10; // static variables are accessed via the class name, NOT a specific INSTANCE of the class
In the code you posted, there is a variable called “buildManager”, and its value is being set to the value of the static variable “BuildManager.instance”. The fact that “buildManager” has the same letters as “BuildManager” has no effect, from the computer’s perspective (the capitalization makes them different words).
Thank you for the responses. From what you have replied I’ve tried forming an understanding on what’s happening. Please correct me where I’m wrong.
This is the full code just in case what I say gets confusing:
public class BuildManager : MonoBehaviour
{
public static BuildManager instance;
public GameObject standardTurrentPrefab;
public GameObject anotherTurrentPrefab;
private GameObject turrentToBuild;
void Awake()
{
if (instance != null)
{
return;
}
instance = this;
}
public GameObject GetTurrentToBuild()
{
return turrentToBuild;
}
public void SetTurrentToBuild(GameObject turrent)
{
turrentToBuild = turrent;
}
}
public class Shop : MonoBehaviour
{
BuildManager buildManager;
void Start()
{
buildManager = BuildManager.instance;
}
public void PurchaseStandardTurrent()
{
buildManager.SetTurrentToBuild(buildManager.standardTurrentPrefab);
}
public void PurchaseAnotherTurrent()
{
buildManager.SetTurrentToBuild(buildManager.anotherTurrentPrefab);
}
}
Here is what I have tried to piece together. Please point out any mistakes:
When the line ‘instance = this’ is executed in the BuildManager class, all the values of the variables in the class are (for lack of better word) saved into ‘instance’. So that would mean that if I run the game and change the values of standardTurrentPrefab and somehow run instance = this, ‘instance’ that was run during Awake() and the ‘instance’ that just run will not be the same? Or since ‘instance’ is static the data saved in it will automatically change without running ‘instance = this’ again. I thinking the latter is correct…
In the Shop class, since buildManager is set to BuildManager.instance, that means any method or variable called by buildManager. accesses the (again for lack of better word) original copy of the method or variable since buildManager is acting as a sort of pointer or reference to the methods and variables of the BuildManager Class?
Apologies if the terms I’ve used aren’t correct, but I hope I’ve made it clear.
Sounds like you aren’t really familiar with how classes work, yet.
Very briefly: A class definition is sort of like a template or blueprint for making a particular type of object. Once you have that definition, you can start churning out a bunch of copies of that object, each with their own, individual set of data. Those copies are called “instances”.
You can store an instance in a variable. When you do that with classes, you aren’t really saving the entire object, you’re just saving a reference to the object; like instructions for where to find it. So if you assign two variables to the same instance, that doesn’t mean you have two objects, it means you have two ways of finding the same object. As a result, if you change the data inside the object from one of those variables, and then read that same data from the other variable, you’ll see the new data.
var x = new MyClass();
x.someVar = 7;
var y = x; // x and y are both references to the same object
print (y.someVar); // prints "7"
x.someVar = 12;
print(y.someVar); // prints "12", because y is referencing the same object as x
You might find it helpful to do some additional reading on classes (just in the context of C#, not Unity specifically).
Thanks Antistone. You’re right I’m not really familiar with classes. I just sort of dove into Unity and C# in hopes of getting started at something, but it seems like I skipped some fundamentals. Aside from classes, is there anything else very fundamental I should look at before I proceed to follow more tutorials? I feel like I might be overlooking some important things by just learning C# as I go.
Another way to think of it, “BuildManager.instance” is a global. You could use “BuildManager.instance.chickenCount” and so on just fine. “buildManager=” is simply making a shortcut. Now “buildManager.chickenCount” is the same thing, pointing at the same global, but shorter.
You know when you have “public GameObject gg;” and you can drag something into that slot in the Inspector? gg becomes a way to find it? Same idea.