Unity Problem : instantiated object considered as null

Hello Everyone. Today, i’m reporting an amazing problem in Unity3D.

In a foreach loop, the temporary variable is considered as null !
Here is the code :

// List<MyClass> evs is not null and not empty.
foreach (MyClass ev in evs)
{
     Debug.Log("object : " + ev + " ; isnull : " + (ev == null) + " ; attribute : " + ev._name); 
}

And here is what i got :
object : null ; isnull : True ; attribute : Object 0

Why is my object ev considered as null where I can access to its attributes ?

Thanks for explaining and sorry for the bad english…

Avalion

The code that shows how you populate the list would be more helpfull :slight_smile: Also, the debugger may not know how to interpret the MyClass object – it does show the name Object 0 instead of some gibberish.

All you have to know is here

  • My Add function.
List<MyClass> evs = new List<MyClass>();
MyClass ev = new MyClass();
ev._name = "Object " + evs.Count;
evs.Add(eo);
  • The Foreach loop is in the Update function

  • I didn’t override ToString() method in MyClass and it does the same with this class !

public class MyClass : MonoBehaviour {
     public string _name;
     public EventObject() {
          _name = "NewObject";
     }
}
  • ev._name works for all the Items and it’s always the same
  • If the debugger don’t know how to interprete MyClass, I guess it doesn’t write “null” in the Log.

Have you pasted or typed the code in here? You have evs.Add(eo) instead of evs.Add(ev).

It’s actually atyping error only in this thread, not in code. Thanks for notification

You are not supposed to instantiate MonoBehaviour based classes. Make MyClass inheriting no class and it’ll work.

Actually you are supposed to instantiate, I think you know the correct solution, but have used the wrong wording.

Look into using Instantiate()

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

If your class inherits from MonoBehaviour you cannot use a constructor to create an instance of it. You must use Instantiate(), or stop inheriting from MonoBehaviour.

Thanks for it :slight_smile: It was the reason.

I saw the warning in Unity but never thought it could make a funny error like this one :stuck_out_tongue:

object : NewObject (MyClass) ; isnull : false; attribute : Object 0

This is an old post, but just in case someone stumbles on it via Google like I did :

I’m almost sure that classes that inherit from UnityEngine.Object overload their == comparison operator, so that it returns true when comparing the current object with null and the Destroy() method has been called on that object. It follows that, assuming you have a valid instance of UnityEngine.Object called myObject at some point in your code, the following …

Debug.Log("is myObject null according to the == operator ? " + (myObject == null ? "Yes" : "No"));
Destroy(myObject);
Debug.Log("is myObject null according to the == operator ? " + (myObject == null ? "Yes" : "No"));

… will output the following in the console :

is myObject null according to the == operator ? No
is myObject null according to the == operator ? Yes

Of course the “real C# object” has not been destroyed : you just passed it to the Destroy function which cannot free memory. Only the Garbage collector can release memory, and it will never do so for an object that is currently accessible by the application code. You also did not change the reference held by the myObject variable. So myObject is still referencing the same memory area occupied by your object. It is therefore NOT a null pointer stricto sensu.

Overriding the “==” operator so that it make your myVariable reference “look like” a null pointer is designed to be a handy trick however : it allows you to test in a very quick and simple way whever a variable points to an existing AND usable object (because once Destroy() has been called on an object it is of course no longer usable in your Unity app). Whever one likes this trick or not is a matter of personal preference …