Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kwokrunner · Feb 12, 2018 at 09:25 PM · errornullreferenceexceptionunassignedreferenceexcept

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); } } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

what does this error mean ? 2 Answers

(2D) Raycast check - null reference even when (RayHit != null) 2 Answers

NullReferenceException trouble 1 Answer

Error in changing color from sendmessage 1 Answer

NullReferenceException error in an array of objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges