- Home /
My script does not find class form another script.
I am making a health bar system for my game, I used a tutorial that i found on YouTube. I get an error saying that the class health bar doesn’t exist
all of my scripts are in the same folder.
this is the script with the class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public Slider slider;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
}
This is the script that is not able to find the class HealthBar:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour {
public int maxHealth = 25;
public int currentHealth;
// this is the problem it is not finding the health bar class
public HealthBar healthbar;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Answer by ADiSiN · May 27, 2020 at 01:26 AM
Hi!
So, I think you mixed up the inheritance and the variable things:
You are creating variable type of HealthBar that must be assigned in Inspector (I assume, since you made it public), so it will not be assigned auto by itself, because you must pass reference into the healthbar variable and the passed Object must contain HealthBar on itself.
However, I suppose the tutorial was about inheritance, so the Health class inherit HealthBar class so the Health class has functions and variables of HealthBar class.
In order to do so you must do it this way:
public class Health : HealthBar
{
public int maxHealth = 25;
public int currentHealth;
}
So your Health class inherit not from MonoBehaviour but from HealthBar therefore contains its variables and functions.
Hope it helps.
Hi! Thanks for your comment. I tried what you told me to do but now I got another error: The type or namespace name could not be found. Any idea why?
Answer by DevGa_me · May 27, 2020 at 05:28 AM
You must assign the healthBar script either in the inspector or in the script itself. You can make a static class if u want to assign it .
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object I re-enable cannot be referenced 0 Answers
Health below zero but shows negative numbers 1 Answer
How to make slider function properly after an animation? 2 Answers
Raycasthit2d isnt working. 2 Answers