Hi, I have fixed this issue, but I am wondering why it doesnt work the way I thought, just for future work.
I am using a list and i wanted something to happen if the list was not empty so i tried using
public List<GameObject> gameObjects;
void Update()
{
if ((gameObjects != null) && (objectExists == false))
{
Debug.Log("In if");
}
}
but apparently it doesnt work that way, using != null. I am used to using c++ and can do that there. But for this i had to do, gameObjects.Count != 0 . I am wondering why? Is it a unity thing that I cant use null, or a c# thing? I thought most data structures would be created the same way even if the language was different?
its simple. when you use a list, the list itself is an object even if you add nothing to it, it has functions and properties etc so it will never be null, just empty, a gameobject for example is just a pointer and unless you fill it with content, its null.
That’s something Unity does automatically so that you can edit that member in the inspector. Even though you didn’t assign a value to gameObjects, it’s public, so that means that unity serialized it to the inspector. The Unity editor created an instance (Look at your component in the editor and you can see it) and assigned it to gameObjects.
If you had marked the member as private, or not serialized, or it was a type that isn’t serializable than the value would still have been null when your code ran.
While vargata is right, I think there’s a need for some additional information.
First of all, yes C# works pretty much similar to C++ in that regard. So generally when you have a field like you have in your sample code, it would be null by default. You “usually” have to exxplicitly create an instance of the List class. This is also generally true in Unity with the exception of serialized objects in Unity.
Unity’s serialization system does support 2 collection types (native C# arrays and the generic List class). When you create an instance of your component in the scene, your class will be serialized into the scene. The inspector in the Unity editor will automatically create arrays and List instances, otherwise you would not be able to interact with them in the inspector.
Though be careful: This only happens in the inspector. For example if you would attach this script component at runtime through code (AddComponent), your “gameObjects” field would actually be null.
So to answer your question, yes it’s essentially a Unity specific behaviour, specifically related to the Inspector and Unity’s serialization system.
No, no… Lists can be null, in fact, in the example code below, the list is declared as null.
When using c#, we can difference between reference values and type values.
Value types behave in a way quite similar to what is expected from the behavior of a field in c ++.
They have a default value, they always take when they’re initialized.
Primitive c# types are value types (int with default value 0, bool with default value false, etc). Also enums and structs (meaning c# structs, don’t think like c++ structs, it’s not the same) (Vector2 and Vector3 for example are structs with default value (0,0) and (0,0,0)).
We’ve pointers at c# in order to manage them as reference values, but you shouldn’t try to use them at your very start, they’re not commonly used and mostly all you can do with pointers you can do using the ref out KeyWords, take an eye to this.
Reference values look somewhat like pointers in c ++.
They are passed by default by reference, however they do not point to any type value, but rather to the memory address to which they were called when they were initialized (surely someone with more knowledge can explain better how this works inside, I don’t get to that much). That is, they handle themselves and destroy the memory where they point automatically when it’s unused (goodbye memory leaks :)).
Classes (c# classes, not the same then c++ ones also, care about this) are reference types and List is a class. By default, if you only declare them they will be null, like pointers they will be referencing to anywhere. So if you want them to have a default value you must indicate it in their declaration.
In summary, if you call public List<GameObject> gameObjects; you will have a null List. This null List points to anywhere so it will drop an error if you try to call .Count or any other property from it. You need to initialize it like public List<GameObject> gameObjects = new List<GameObject>();