- Home /
Object says it's null, but on inspecting it seems to have the correct value
Hi, I have a derived class that I'm serializing. If I check the value in code, it says it's null. But if I inspect the value it actually has a value. See attached image. Can anyone tell me what's causing this?
As you can see, the if-check succeeds and the for-loop is short-circuited with a 'continue', however 'item' actually has a value, the value that I'd expect it to have.
[Serializable]
public class ScriptableActionsAsset : NarrationScriptableObject
{
[SerializeField]
public ScriptableActionItem[] Items;
}
[Serializable]
public class PlayAudioActionItem : ScriptableActionItem
{
[SerializeField]
public AudioClip pathToAudioClip;
}
[Serializable]
public class ScriptableActionItem : ScriptableObject
{
}
I realise that the [Serializable] attributes are a bit redundant, but I had them in initially and haven't removed them yet. The ScriptableActionItem and NarrationScriptableObject contains methods but no data, they both extend ScriptableObject.
The ScriptableActionsAsset is a property on a MonoBehaviour. The Start() method of MonoBehaviour (attached to a GO) runs the following method:
public void ConvertActionItems(GameObject target, IList items)
{
foreach(ScriptableActionItem item in items)
{
if (item == null)
{
continue;
}
var scriptableItem = item.ToAction(target);
Actions.Add(scriptableItem);
}
}
am I blind or does the inspector say the value of item is {null}?
Yes, but... it says it has a base and the property (pathToAudioClip) is correct and is a member of the class I'm expecting. The class itself extends from ScriptableActionItem (my own class) and that extends from ScriptableObject.
item is null ...
Everything after that is not relevant for you, you only check the item value.
I don't understand why it's null but I can see the values I'm expecting though. If I go to the immediate window and type 'item == null' (without quotes) it returns false. Similarly, if I type item.pathToAudioClip I get the value I expect.
If you are sure that "item" is valuated, you'll need to show us more of your code, to check what is the real value.
Answer by Lovrenc · Aug 09, 2013 at 11:23 PM
Item is null!
That value you see simply tells you the TYPE OF YOUR VARIABLE.