I keep getting the error
:
NullReferenceException: Object reference not set to an instance of an object
BuildingStore.OnGUI () (at Assets/BuildingStore.cs:31)
The code it is referencing is:
void OnGUI()
{
if (SelectionManager.me.selectionMode == selectingModes.creatingBuildings) {
int yMod = 0;
foreach (GameObject b in buildings) {
try {
Building buildingScr = b.GetComponent<Building> ();
Rect pos = new Rect (50, 50 + (50 * yMod), 100, 50);
if (GUI.Button (pos, b.name)) {
selectedBuilding = b;
}
yMod+=1;
} catch {
Debug.Log ("Building Missing A Component");
}
}
}
}
the “if (SelectionManager.me.selectionMode == selectingModes.creatingBuildings) {” line is line 31.
Also, this section of the code is not working.
can anyone tell me what the error is trying to say to me.
SelectionManager.me is a reference to another script called SelectionManager. In it there is a enum which is what is being referenced with “SelectionManager.me.selectionMode”. When I change the enum to “creating buildings” the if statement should work.
It looks to me like SelectionManager.me is set to null. You should check it. If selectingModes is enum, then there is nothing else on that line that could cause such an exception.
You probably forgot to instantiate SelectionManager, something like
I tried to put in me = new SelectionManager (); in the void awake but now its giving me the error:
You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
if you are using “public static SelectionManager me;” in the script then you need to set the instance me in the same script itself
void Awake()
{
if(me==null)
me=this;
else if(me!=null)
me=this;
}
And if you are referencing a script without static instantiation,
public GameObject referencedObject;
//where referencedObject is the gameObject onto which SelectionManager is attached to.