Null reference with SerializedProperty

Hey Everyone!

To start off, i’m kinda new to serializedobjects and properties.

I´ve got this weird problem where i want to get an array from a GUI. The syntax is as following in the GUI script.

#pragma strict
import App;

var apps : App[];

Then when I try to access that array of Apps using the following code:

class GUIEditor extends Editor {
	//Apps
    var apps        : SerializedProperty;
    function OnEnable () {
        // Setup the SerializedProperties
		Debug.Log(serializedObject.FindProperty("apps"));
        apps        = serializedObject.FindProperty("apps");
		
    }
}

As soon as i try to print out the value of apps using Debug.Log(), Null gets returned, even-though the variable exists.

You guys have any idea on why it does that and how to fix it?

EDIT:

Code for the class App.cs

using UnityEngine;
using System.Collections;

public class App {
	private string app_name = "undifined";
	private Texture2D icon;
	private Texture2D background;
	private bool show = false;
	public App() {
	}
	public bool Show {
		get { return show; }
		set { show = value;}
	}
	public string Name {
		get { return app_name; }
		set { app_name = value; }
	}
	public Texture2D Icon {
		get { return icon; }
		set { icon = value; }
	}
	public Texture2D Background {
		get { return background; }
		set { background = value; }
	}
}

What is App? is it a type that can be serialized by Unity? Without your custom editor, does the apps array show up in the inspector? If not App is probably not serializable.

App is a custom class. When I display the apps array in the inspector it does work, but i want to do things a little bit easier for other people, hence the custom editor

1 Answer

1

I’m a bit confused. Your class above is UnityScript and your editor as well, but the App class is C#?

You said that the “apps” array showed up in the inspector without your editor, but that’s not possible since the App class:

  • isn’t marked as Serializable.
  • doesn’t contain any data that is serialized by Unity

Only public variables or private variables with the SerializeField attribute are serialized. Unity can’t serialize properties.

Take a closer look at the SerializeField page, it explains most things quite detailed.