- Home /
Null Reference Exception/Unassigned gameobject error
I have had endless Null Reference Exception errors in making the UI for my game, which probably means I have some sort of basic misunderstanding of how Unity C# works. My current problem is that when I try to click on a gameobject (Def1) I want it to affect another gameobject that was previously clicked (listOfLocations[i]). However, this isn't working because I assign which gameobject within GameObject[] listOfLocations was last clicked to an exterior code, which is referenced in the script as gamecontroller. In the method that assigns this, it is working and I am able to print gamecontroller.GetComponent.currentLocationName(). Yet when I click on that other gameobject and run Def1Upgrade method suddenly there's the error "The variable gamecontroller has not been assigned". I don't know why. Help please.
Here's my code: //This creates GameObject for specific location public GameObject gamecontroller; public GameObject da_Location; GameObject[] listOfLocations = new GameObject[24];
//Player Stuff
public GameObject Player1;
public GameObject Player2;
//Basic Templates for a Location
public GameObject tem_Un;
public GameObject tem_Fort;
public GameObject tem_Min;
public GameObject tem_Tem;
//Location Values
string theName;
bool isUnsettled;
bool isFort;
bool isMine;
bool isTemple;
int defLvl;
int trainLvl;
int goldLvl;
int armLvl;
int devLvl;
int fervLvl;
double Health;
double TrainRate;
public double WealthGen;
public double FaithGen;
//Unsettled Buttons
public GameObject objChooFor;
public GameObject objChooMin;
public GameObject objChooTem;
public Button chooFor;
public Button chooMin;
public Button chooTem;
// Use this for initialization
void Start () {
isUnsettled = true;
isFort = false;
isMine = false;
isTemple = false;
defLvl=0;
trainLvl=0;
foreach (GameObject s in GameObject.FindGameObjectsWithTag("Locationz")){
for (int i = 0; i < 24; i++) {
if (listOfLocations [i] == null) {
listOfLocations [i] = s;
break;
}
}
}
//These methods set the specific class for each location
void CchooseFort (){
tem_Fort.gameObject.SetActive (true);
for (int i = 0; i < 24; i++) {
if (listOfLocations [i].name == gamecontroller.GetComponent<StartGayme>().currentLocationName) {
listOfLocations[i].GetComponent<LocationCode>().isUnsettled = false;
listOfLocations[i].GetComponent<LocationCode>().isFort = true;
listOfLocations [i].GetComponent<LocationCode>().Health = 400;
listOfLocations [i].GetComponent<LocationCode>().TrainRate = 1;
}
}
tem_Un.gameObject.SetActive (false);
}
//Fort Upgrades
public void Def1Upgrade (){
for (int i = 0; i < 24; i++) {
print (theName);
print (gamecontroller.GetComponent<StartGayme> ().currentLocationName);
if (listOfLocations [i].name == gamecontroller.GetComponent<StartGayme>().currentLocationName && defLvl==0) {
print ("anything");
listOfLocations [i].GetComponent<LocationCode>().Health += 150;
listOfLocations [i].GetComponent<LocationCode> ().defLvl = 1;
Player1.GetComponent<Player>().setWealth(Player1.GetComponent<Player>().getWealth()-250);
print (listOfLocations [i].name + " has train rate " + listOfLocations [i].GetComponent<LocationCode> ().trainLvl.ToString ());
print (listOfLocations [i].name + " has defense " + listOfLocations [i].GetComponent<LocationCode> ().defLvl.ToString ());
//Reduce alpha of Def1 Image OR make Def1 Image grayed-out/darker;
}
}
}
//sets which location gameobject was clicked last and identifies what type it is public void OnMouseDown () { gamecontroller.GetComponent().currentLocationName = this.gameObject.name; theName = gamecontroller.GetComponent ().currentLocationName; //print (gamecontroller.GetComponent().currentLocationName); print (theName); tem_Un.gameObject.SetActive (false); tem_Fort.gameObject.SetActive (false); tem_Min.gameObject.SetActive (false); tem_Tem.gameObject.SetActive (false); if (this.isUnsettled == true) { tem_Un.gameObject.SetActive (true); } if (this.isFort == true) { tem_Fort.gameObject.SetActive (true); } if (this.isMine == true) { tem_Min.gameObject.SetActive (true); } if (this.isTemple == true) { tem_Tem.gameObject.SetActive (true); } } }