Object Reference Error
I'm creating pickups that will modify my HUD text once they are picked up. Instead, they are just giving me object reference errors. Any tips would be appreciated!
public void ModHealth(int _value)
{
CurHealth += _value;
PlayerHUD.updateHealth(CurHealth);
}
This is one of the examples of it going wrong. I have it Curhealth setup with a int as well as having a PlayerHUD reference. What could be the issue whenever I pick up an item?
That little blurb of code is not enough so that anyone can help you.
One needs at a $$anonymous$$imum the script with the code you are showing and the PlayerHUD script could help as well.
Sorry! The scripts that they are contained in are pretty long so I didn't know if they would be able to be pasted. Let me try and make an imgur of them.
Here is the code that is related to my player script.
[SerializeField] int $$anonymous$$axLives = 3; private int CurLives = 3;
//Player: Stats: $$anonymous$$ax & Current Ammo
[SerializeField]
int $$anonymous$$axAmmo = 50;
private int CurAmmo = 50;
//Player: Stats: $$anonymous$$ax & Current Shields
[SerializeField]
int $$anonymous$$axShields = 100;
private int CurShields = 100;
//Player: Stats: $$anonymous$$ax & Current Health
[SerializeField]
int $$anonymous$$axHealth = 3;
private int CurHealth = 3;
void OnTriggerEnter(Collider other)
{
if (other.tag == "PickUp")
{
if (other.name == "Score")
{
$$anonymous$$odScore(5);
Destroy(other.gameObject);
}
if (other.name == "Life")
{
$$anonymous$$odLives(1);
Destroy(other.gameObject);
}
if (other.name == "Health")
{
$$anonymous$$odHealth(1);
Destroy(other.gameObject);
}
if (other.name == "Shield")
{
$$anonymous$$odShields(25);
Destroy(other.gameObject);
}
if (other.name == "Ammo")
{
$$anonymous$$odAmmo(10);
Destroy(other.gameObject);
}
if (other.name == "Negative")
{
$$anonymous$$odShields(25);
Destroy(other.gameObject);
}
else if (other.tag == "Basic")
{
$$anonymous$$odShields(-10);
}
else if(other.tag == "Fighter")
{
$$anonymous$$odHealth(-1);
}
else if(other.tag == "Strong")
{
$$anonymous$$odLives(-1);
}
}
}
//HUD: Player: Updates Ammo
public void $$anonymous$$odAmmo(int _value)
{
CurAmmo += _value;
PlayerHUD.updateAmmo(CurAmmo);
}
//HUD: Player: Updates Health
public void $$anonymous$$odHealth(int _value)
{
CurHealth += _value;
PlayerHUD.updateHealth(CurHealth);
}
//HUD: Player: Updates Shields
public void $$anonymous$$odShields(int _value)
{
CurShields += _value;
PlayerHUD.updateShield(CurShields);
}
public void $$anonymous$$odScore(int _value)
{
Score += _value;
PlayerHUD.updateScore(Score);
}
public void $$anonymous$$odLives(int _value)
{
CurLives += _value;
PlayerHUD.updateLives(CurLives);
}
Because PlayerHUD is not declared I must assume that it is static.
If that is the case then the problem is in the PlayerHUD script which I need to see.
Otherwise it needs to be declared somehow. But what is important is where is the script attached relative to the player script. It may as simple as addiny a property in the player script above and set it in the inspector.
I need to see the whole class, not just the contents (that goes for each class)
In other words I need to see all this (for example)
using UnityEngine;
using System.Collections;
public class $$anonymous$$yClass : $$anonymous$$onoBehaviour
{
// yada yada yada
}
$$anonymous$$y apologies, this is the everything for the HUD
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HUD : $$anonymous$$onoBehaviour {
public Text$$anonymous$$esh scoreText$$anonymous$$esh;
private string scoreText;
public Text$$anonymous$$esh ammoText$$anonymous$$esh;
private string ammoText;
public Text$$anonymous$$esh shieldText$$anonymous$$esh;
private string shieldText;
[SerializeField]
public int $$anonymous$$axAmmo = 50;
[SerializeField]
public int Ammo = 10;
public LifeIcon lifeIconObj;
private List<LifeIcon> lifeIconInstances;
public HealthIcon healthIconObj;
private List<HealthIcon> healthIconInstances;
// Use this for initialization
void Start () {
scoreText = scoreText$$anonymous$$esh.text;
//HUD: Ammo Count: Percent
updateAmmo(Ammo = Ammo / $$anonymous$$axAmmo);
CreateLifeIcons(3);
CreateHealthIcons(3);
}
//HUD:Ammo Count
public void updateAmmo(int value)
{
ammoText$$anonymous$$esh.text = ammoText;
ammoText$$anonymous$$esh.text += value;
}
public void updateScore(int value)
{
scoreText$$anonymous$$esh.text = scoreText;
scoreText$$anonymous$$esh.text += value;
}
//HUD:Shield Strength
public void updateShield(int value)
{
shieldText$$anonymous$$esh.text = shieldText;
shieldText$$anonymous$$esh.text += value;
}
public void updateLives(int numLivesRemaining)
{
for (int i = 0; i < lifeIconInstances.Count; ++i)
{
bool bActive = i < numLivesRemaining;
lifeIconInstances[i].gameObject.SetActive(bActive);
}
}
public void updateHealth(int numHealthRemaining)
{
for (int i = 0; i < healthIconInstances.Count; ++i)
{
bool bActive = i < numHealthRemaining;
healthIconInstances[i].gameObject.SetActive(bActive);
}
}
and this is just the class for the player(first code I posted)
using UnityEngine;
using System.Collections;
public class PlayerShipController : $$anonymous$$onoBehaviour
{
Answer by TBruce · Jun 13, 2016 at 12:12 AM
So the problem seems to be that you are not declaring a reference to HUD like so
public HUD PlayerHUD; // this is set in the inspector
void Start()
{
if (PlayerHUD == null)
{
Debug.LogError("The PlayerHUD property has not been set in the inspector. Please set now and try again.");
}
}
or like this
private HUD PlayerHUD;
void Start()
{
if (GetComponent<HUD>() != null)
{
PlayerHUD = GetComponent<HUD>();
}
else
{
Debug.LogError("Missing PlayerHUD component. Please attach one and try again.");
}
}
I'm absolutely silly, I never set my HUD inside my inspector when I opened my new scene. Thank you for re$$anonymous$$ding me with the debug LOL.