- Home /
Using reference without object (no new)?
I have question for code from this tutorial: http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player There is class definition for Boundary. There is reference created (called boundary). But there is no "boundary= new Boundary()" line.
Looks like I miss some very important (but basic) concept.
On the other hand, in another tutorial: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes 'new' is called for class Stuff.
Answer by RudyTheDev · Dec 22, 2014 at 08:11 PM
You are right, and you would almost always create new classes with new
.
But here it works, because Boundary
is marked as Serializable
and so Unity would create a new
instance whenever it (de)serializes it (to be precise, whenever it (de)serializes PlayerController.boundary
). So, when you look into the inspector of PlayerController
, it will just "magically" be there, because opening inspector performs serialization and creates it.
It is easily possible to break this:
public class BreakPlayerControllerBoundary : MonoBehaviour
{
void Start()
{
PlayerController playerController = gameObject.AddComponent<PlayerController>();
Debug.Log(playerController.boundary.xMax); // NULL REF EXC
}
}
Here you would get a NullReferenceException
on playerController.boundary
, because it was never (de)serialized by Unity and so is still null
.
More in-depth info: Serialization in Unity.
Thank You for your explanation. I have 2 more questions:
I mark class Boundary Serializable and then I create an object with new. Does it make any sense? Will Unity still create a new instance of the class?
Why Unity will never (de)serialize playerController.boundary in second example? The class is marked as Serializable.
[Serializable]
does not mean that it will serialize right away (or ever). It implies that when Unity (de)serializes, one of things is that it will create any custom serializable classes ins$$anonymous$$d of keeping them null
. In many cases, such as AddComponent<>
, Unity does not perform serialization, so the custom class isn't created.
Unity won't overwrite your class if you create it yourself with new
. In that example, you can do this:
PlayerController playerController = gameObject.AddComponent<PlayerController>();
playerController.boundary = new Boundary();
Debug.Log(playerController.boundary.x$$anonymous$$ax); // Okay
You could also define it as:
public Boundary boundary = new Boundary();
Or even better you could add it to Awake()
(which happens right after AddComponent
):
void Awake()
{
boundary = new Boundary();
}
Answer by Owen-Reynolds · Dec 24, 2014 at 05:29 PM
Unity sets up (new's, initializes) all global public vars for you. boundary
is a public variable in that script: public Boundary boundary;
.
What happens is, when you first add public Boundary boundary;
, Unity displays it in the Inspector. That's a nice feature, so even a non-programmer can easily give it some starting values. It starts with all 0's, because it has to start somewhere.
It assumes you're going to type the correct xMin, xMax ... into the Inspector. Then, when the game runs, before the script starts, Unity new's boundary
and copies in your numbers. It has to, since how else would the numbers magically get there?
Even if you never change the numbers, since it's public and you can see those 0's in the Inspector, the deal is that you get it set up with those 0's.
The exact way Unity stores the Inspector data is by serializing. If you make a public var in a class that can't be Serialized (saved,) Unity can't save the values, so there's no point showing it in the Inspector.