• Advertisement
Sign in to follow this  

Mind blow null object

This topic is 594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

J1_Lt1e5.png
This blow my head off because I do not understand why I can access information in a null object, and because I don't not know what keyword to put in google to search so decide to ask here. thanks   
 
And I just added these line below
 
if (currentSelectedCard [0] == null) {
Debug.Log("null");
}
 
and the console prints out null , How can I go around it in this case
Edited by KitKat20152016

Share this post


Link to post
Share on other sites
Advertisement
Your link is broken, remove the trailing two characters.

Regarding the nulls, I'm not entirely sure, but my educated guess goes along this: With LinQs late evaluation you may run into situations where the debugger may not show the true story.

Share this post


Link to post
Share on other sites

So if I'm correct does that mean if this class/script never attach to any gameobject this will be always null ? 

for example : List<SYC_BusinessDB> m_cardDB , and in my game I have create some objects of SYC_BusinessDB and put it in m_cardDB but however I have never any SYC_BusinessDB to any GameObject so because of that it destroy the c++ side is that correct ?

Share this post


Link to post
Share on other sites
If you're using 'new SYC_BusinessDB' and SYC_BusinessDB is derived from MonoBehaviour, then Unity should be printing errors/warnings about that. I don't remember off the top of my head if improperly created MonoBehaviours start off "null" or not, but you might be right.

If your SYC_BusinessDB class doesn't ever need to be attached to a gameObject as a component, just don't derive from MonoBehaviour. You'll get a "POCO" - a "Plain Old C# Object". Those will work just like C# objects do in non-Unity projects and don't have any of the weird "pretends to be null" stuff.

If you DO need it to be attached to a gameObject as a component, then you'll need to properly instantiate it using one of Unity's methods, like GameObject.AddComponent<T>, instead of using 'new'. Edited by Nypyren

Share this post


Link to post
Share on other sites

For The Record -

I Have A Reference In My C# Script That Confuses Me Too
1. I'm Using "Object A" Transform To Move "Object B"

2. I Destroy "Object B" That's Referenced In That Class

3. I Then Pass A New Instance INTO THE DESTROYED "Object B" - Now It's "Object C"

 

ALL I KNOW IS IT WORKS.
AND I CAN'T EVEN EXPLAIN IT.

Edited by EscondidoZombieFactory

Share this post


Link to post
Share on other sites

ALL I KNOW IS IT WORKS. AND I CAN'T EVEN EXPLAIN IT.
 

 

Post code, preferably to a new topic since this one is about an issue that is already discussed.  

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement