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:
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:
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.