Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by Anymeese · Jun 02, 2014 at 11:02 PM · c#variableprogrammingvariables

[C# Code] Why am I getting this error???

I have a function to update a placeholder UI made up of 4 3d text objects (sorry for the long names):

 private void updateUnitStatsUIWindow(ArmyUnit script)
 {
 costs.GetComponent<TextMesh>().text = "Costs (M - A): " + script.getMoveCost() + " - " + script.getAttackCost();
 atkInfo.GetComponent<TextMesh>().text = "Range - Dmg: " + script.getAttackRange() + " - " + script.getAttackDamage();
 health.GetComponent<TextMesh>().text = "Health: " + script.getCurrHealth() + " / " + script.getMaxHealth();
 level.GetComponent<TextMesh> ().text = "Level (Exp): " + script.getLevel() + " (" + script.getExperience() + " / " + script.getExpNeededForNextLevel() + ")";
 }

Which results in something like this (top left corner): http://i.imgur.com/yHI85Gb.jpg

I call the function defined above when a unit is selected and when making the unit (because it is selected when it's first made). ArmyUnit is a script attached to the unit that has all the get functions.

1) here is the code for when the unit is made:

 //selects the newly created unit and sets the stats at the top left to its stats
 public void onNewUnitCreation(GameObject newUnit)
 {
     currentlySelectedUnit = newUnit;

     showUnitSelectionMarker(newUnit);
     updateUnitStatsUIWindow(currentlySelectedUnit.GetComponent<ArmyUnit>());
     statsWindowUIPLACEHOLDER.SetActive(true);
 }

2) here is the code for when the unit is SELECTED:

 if(Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, 500f, gameControl.getUnitLayer()))
     {
         currentlySelectedUnit = hit.collider.gameObject;
         showUnitSelectionMarker(hit.collider.gameObject);
         updateUnitStatsUIWindow(currentlySelectedUnit.GetComponent<ArmyUnit>());
         statsWindowUIPLACEHOLDER.SetActive(true);
     }

PROBLEM: Whenever the unit is SELECTED I get an error. Whenever the unit is CREATED, I do not. WHY?

THE ERROR:

 NullReferenceException: Object reference not set to an instance of an object
 SingleUnitControl.updateUnitStatsUIWindow (.ArmyUnit script) (at Assets/Scripts/SingleUnitControl.cs:99)
 SingleUnitControl.Update () (at Assets/Scripts/SingleUnitControl.cs:43)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Anymeese · Jun 03, 2014 at 01:07 AM

After another hour or two of trying everything, I (somewhat) discovered the problem. Had to do with the collider that I use when checking for mouse over/click being on a child of the unit rather than the part game object. Solved by making a larger collider on the parent (and would delete the other collider if I could find the damn thing)

Comment
Add comment · Share
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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

How to use variable in MailMessage To? 1 Answer

Distribute terrain in zones 3 Answers

Variable not showing update in inspector 2 Answers

Problem with getting a value from a enum 2 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