Hello. I am extreamly new to coding, but as far as I can tell this script should work.
public class HeartsGUI2 : MonoBehaviour {
public GameObject Player22;
public GUITexture image2;
public Texture2D Image_1;
public Texture2D Image_2;
public Texture2D Image_3;
public Texture2D Image_4;
public Texture2D Image_5;
public Texture2D Image_6;
public int hp = 5;
public bool isEnemy = true;
void Awake()
{
Player22 = GameObject.Find("Player2");
}
void Update()
{
if (Player22.GetComponent<Health>().hp == 5) {
image2.guiTexture.texture = Image_1;
}
if (Player22.GetComponent<Health>().hp == 4) {
image2.guiTexture.texture = Image_2;
}
if (Player22.GetComponent<Health>().hp == 3) {
image2.guiTexture.texture = Image_3;
}
if (Player22.GetComponent<Health>().hp == 2) {
image2.guiTexture.texture = Image_4;
}
if (Player22.GetComponent<Health>().hp == 1) {
image2.guiTexture.texture = Image_5;
}
if (Player22.GetComponent<Health>().hp <= 0) {
image2.guiTexture.texture = Image_6;
}
{
}
}
}
Basically the code is supposed to change the image displayed on a hearts GUI every time Player2 is shot to show how much health it has left. I made the same code referencing my Player1 and it works fine but when I copy pasted it over and renamed everything it no longer worked. Unity came up with an enormous amount of errors all saying the same thing. NullReferenceException: Object reference not set to an instance of an object
HeartsGUI2.Update () (at Assets/Scripts/HeartsGUI2.cs:28). Please help. Thank you.
Ok I am an idiot for not noticing I accidentally named it Player 2 instead of Player2. The script still isn’t working though although it now shows no errors. I don’t understand why it doesn’t work here but does on Player1. I will post my first script. See if you can find what the difference is between the 2. Thank you.
using System.Collections;
public class HeartsGUI : MonoBehaviour {
public GameObject Player1;
public GUITexture image;
public Texture2D Image_01;
public Texture2D Image_02;
public Texture2D Image_03;
public Texture2D Image_04;
public Texture2D Image_05;
public Texture2D Image_06;
public int hp = 5;
public bool isEnemy = true;
void Awake()
{
Player1 = GameObject.Find("Player");
}
void Update()
{
if (Player1.GetComponent<Health>().hp == 5) {
image.guiTexture.texture = Image_01;
}
if (Player1.GetComponent<Health>().hp == 4) {
image.guiTexture.texture = Image_02;
}
if (Player1.GetComponent<Health>().hp == 3) {
image.guiTexture.texture = Image_03;
}
if (Player1.GetComponent<Health>().hp == 2) {
image.guiTexture.texture = Image_04;
}
if (Player1.GetComponent<Health>().hp == 1) {
image.guiTexture.texture = Image_05;
}
if (Player1.GetComponent<Health>().hp <= 0) {
image.guiTexture.texture = Image_06;
}
{
}
}
}
Probably the Health component is not attached to your player2.
Just so you’re aware also, you can store a reference to your Health component on Awake() also. Which will clean your code up a bit.
using System.Collections;
public class HeartsGUI : MonoBehaviour {
public GameObject Player1;
private Health m_health;
public GUITexture image;
public Texture2D Image_01;
public Texture2D Image_02;
public Texture2D Image_03;
public Texture2D Image_04;
public Texture2D Image_05;
public Texture2D Image_06;
public int hp = 5;
public bool isEnemy = true;
void Awake()
{
Player1 = GameObject.Find("Player");
m_health = Player1.GetComponent<Health>();
if(!m_health)
{
Debug.LogError("Health component not attached");
}
}
void Update()
{
if (m_health.hp == 5) {
image.guiTexture.texture = Image_01;
}
if (m_health.hp == 4) {
image.guiTexture.texture = Image_02;
}
if (m_health.hp == 3) {
image.guiTexture.texture = Image_03;
}
if (m_health.hp == 2) {
image.guiTexture.texture = Image_04;
}
if (m_health.hp == 1) {
image.guiTexture.texture = Image_05;
}
if (m_health.hp <= 0) {
image.guiTexture.texture = Image_06;
}
{
}
}
}
Also, are all of your Image_ the same texture? Are you just trying to draw a heart for every hp you have?
No. I have 6 images that I want to show on the GUI Texture depending on the health. My Health script is attached to player2 as well. I got the script to work now. Thank you for your help
How are you drawing them? Maybe they are positioned on top of each other so it looks like only 1 set is drawing.
Your code design is also not great. You should really only have 1 HeartsGUI component. Then your player class would reference this and feed it it’s own health. When I get back from work I may give you an example code.