This question was
closed Feb 21, 2017 at 05:23 PM by
MatheusDomingos for the following reason:
The question is answered, right answer was accepted
Question by
MatheusDomingos · Feb 21, 2017 at 02:17 PM ·
c#custom editor
Editor Window script getting values from another script
Hey guys, I'm doing a custom editor and I need to show some values from another script, but always when I try, it return me the following error, NullReferenceException: Object reference not set to an instance of an object.
Here's my editor script:
using UnityEngine;
using UnityEditor;
public enum enumType
{
player,
itens
}
public class DataBaseEditor : EditorWindow {
bool playerGroup = false;
bool itenGroup = false;
public enumType type;
private PlayersDatabase data;
//CREATE WINDOW
[MenuItem ("Window/Database Editor")]
static void Init()
{
DataBaseEditor window = (DataBaseEditor)EditorWindow.GetWindow (typeof (DataBaseEditor));
window.Show();
}
void Start()
{
data = GameObject.FindGameObjectWithTag("Game Controller").GetComponent<PlayersDatabase>();
}
//CONTENT
void OnGUI()
{
//DROPDOWN LIST
type = (enumType) EditorGUILayout.EnumPopup(type);
if(GUILayout.Button("Select"))
dropDown(type);
if(playerGroup)
{
Rect r = (Rect)EditorGUILayout.BeginVertical();
GUILayout.Label("NAME", EditorStyles.boldLabel);
GUILayout.Label("Max HP: ");
EditorGUILayout.IntField(data.maxHP[1]);
EditorGUILayout.EndVertical();
}
if(itenGroup)
{
Rect r = (Rect)EditorGUILayout.BeginVertical();
GUILayout.Label("Item");
EditorGUILayout.EndVertical();
}
}
void dropDown(enumType selected)
{
switch(selected)
{
case enumType.player:
playerGroup = true;
itenGroup = false;
break;
case enumType.itens:
playerGroup = false;
itenGroup = true;
break;
}
}
}
And here's my "Database" script
using UnityEngine;
public class PlayersDatabase : MonoBehaviour {
public static PlayersDatabase control;
//PLAYERS INDEX
public int[] playerIndex;
//BASIC STATS
public int[] maxHP;
public int[] maxMP;
public int[] maxXP;
public int[] bAtk;
public int[] bMAtk;
public int[] bDef;
public int[] bMDef;
public int[] bStr;
public int[] bItl;
public int[] bAcc;
public int[] bCrt;
public int[] bAgi;
public int[] bEva;
void Awake()
{
if(control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if(control != this)
Destroy(gameObject);
playerIndex[0] = 1;
maxHP[1] = 200;
}
}
The error is on line 44.
Comment
Best Answer
Answer by Adam-Mechtley · Feb 21, 2017 at 02:49 PM
EditorWindow
does not have a Start callback, so I'm assuming your data
field is never assigned, and hence is always null. I would instead suggest you add a control at the beginning of your OnGUI callback to load in the specific PlayersDatabase
you're wanting to inspect, and exit the OnGUI callback early if it is null.