- Home /
[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)
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)
Your answer
Follow this Question
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