0
\$\begingroup\$

I'm currently making a collectible heart in unity, the kind that updates your health bar when you collide with it, and I made two scripts:

This one is called 'healthmonitor':

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class healthmonitor : MonoBehaviour {

public static int HealthValue;
public int InternalHealth;
public GameObject Heart1;
public GameObject Heart2;
public GameObject Heart3;


void Start () {
    HealthValue = 1;
}

void Update () {
    InternalHealth = HealthValue;

    if (HealthValue == 1) {
        Heart1.SetActive (true);
    }
    if (HealthValue == 2) {
        Heart2.SetActive (true);
    }
    if (HealthValue == 3) {
        Heart3.SetActive (true);
    }

}
}

This one is called 'hearstscript':

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 public class heartscript : MonoBehaviour {

public int Rotatespeed;
public AudioSource CollectSound;
public GameObject thisheart; 


void Update () {
    Rotatespeed =10;
    transform.Rotate(0, Rotatespeed, 0, Space.World);
}


void OnTriggerEnter () {
    CollectSound.Play ();
    HealthMonitor.HealthValue += 1;
    thisheart.SetActive (false);
}
}

Here is a picture of my UI:

enter image description here

I made an empty game object and named it HealthMonitor and applied the healthmonitor script to it. Here's a picture of the inspector panel:

enter image description here

The problem is that with heartscript I get the following error;

Name "HealthMonitor" doesn't exist in current context.

I believe it relates to this line:

HealthMonitor.HealthValue += 1;

P.S. I am trying to refer to the empty game object, not the script.

\$\endgroup\$
1
  • \$\begingroup\$ The problem is that your class is with lowercase h, the file/class must be named with capital h -> HealthMonitor \$\endgroup\$
    – Sidar
    Dec 28, 2018 at 19:14

1 Answer 1

2
\$\begingroup\$

Just like the error message says,

Name "HealthMonitor" doesn't exist in current context.

ie. "You haven't told me anywhere in this script what the word HealthMonitor is supposed to mean"

If you want it to refer to the healthmonitor class, then you need to use the class's name exactly, all lowercase the way you've defined it.

But don't do that - making the HealthValue static is not a good solution to this problem, as it limits you to only ever having one instance of this health, and you'll have to refactor if you ever add multiplayer / NPCs with health / etc.

If you want it to refer to a particular instance of the healthmonitor script, (and get rid of the static on HealthValue so it lives on that instance), then you need to tell the compiler/runtime that's what you want.

First, by defining a member variable with the name you want to use, at the top of your heartscript class where the other public member variables are defined:

// This tells the compiler "I'm going to use the name HealthMonitor
// to refer to an instance of the healthmonitor script. And I want this
// variable to be public so I can set the instance in the inspector.
public healthmonitor HealthMonitor;

Then, you need to assign a reference to it. The easiest/most reliable & flexible way to do this is via the inspector. You can just drag the object containing the healthmonitor script from the hierarchy window into the HealthMonitor variable in the heartscript component's inspector. Or click the little circle next to the variable to select it from a list. Note that both instances need to exist inside the same scene or the same prefab for this to work as desired.

If you need to wire up the reference at runtime, you can do it like this if you have just one healthmonitor in your scene:

void Start() {
    HealthMonitor = FindObjectOfType<healthmonitor>();
}

As an aside, I'd recommend following Unity coding conventions where the names of types (like classes / structs) follow PascalCase (capitalize every word, including the first), and the names of member variables follow camelCase (capitalize every word after the first).

\$\endgroup\$
7
  • \$\begingroup\$ I was attempting to refer to the game object HealthMonitor, also when I tried putting 'public healthmonitor HealthMonitor' I got an error saying 'Static member 'healthmonitor.HealthValue' cannot be accessed with an instance reference, qualify it with a type name instead' \$\endgroup\$
    – romoon
    Dec 28, 2018 at 19:46
  • \$\begingroup\$ Remember, a game object's name is not automatically a variable name. You still need to declare a variable and wire up a reference to the GameObject / component you want it to refer to. \$\endgroup\$
    – DMGregory
    Dec 28, 2018 at 19:48
  • \$\begingroup\$ Would you mind updating your answer with the code I could use to do that? \$\endgroup\$
    – romoon
    Dec 28, 2018 at 20:03
  • \$\begingroup\$ You already have it. Search by type, not by name. Looking up GameObjects by name is...error-prone. \$\endgroup\$
    – DMGregory
    Dec 28, 2018 at 20:05
  • \$\begingroup\$ With regard to "static member cannot be accessed with an instance reference" - you remembered to follow my instruction above to get rid of the static keyword where you've declared HealthValue? \$\endgroup\$
    – DMGregory
    Dec 28, 2018 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .