2D Game - Powerbar for special attacks

Hi, im writing a 2D Game and I have a power bar which shall be filled when an enemy is killed. The problem it isnt working. Oh and my Death Animation of the enemy is also not working, maybe you can help here too. Here’s my code:

public class Enemy : MonoBehaviour
{
    public float health = 1;
    public bool isDead = false;
    public float damage = 1;
    Animator anim;
    MummyWalk walk;
    HealthController healthController;
    public float givePower = 1;

    void ApplyDamage(float damage)
    {
        health -= damage;

        if (health <= 0)
        {
            Debug.Log("ApplyDamage geht");
            isDead = true;
          
            Death();
           
        }
    }
   
    void Death()
    {
        Debug.Log("Death Funktion geht");
        isDead = true;
        anim.SetTrigger("mummy_death");
        walk.enabled = false;
        healthController.AddPower(givePower);
    }
 

public class HealthController : MonoBehaviour
{
    public float power = 5;
    public float startPower = 5;

void Start ()
    {
        healthcontroller = this;
        anim = GetComponent<Animator>();
        playerController = GetComponent<Walk>();
       
        if(Application.loadedLevel == 0)   
        {
            power = startPower;
        }
        else
        {
            power = PlayerPrefs.GetFloat("power");
            }
       
        UpdateView();
void UpdateView()
    {
        powerGUI.fillAmount = power / maxPower;
    }

    public void AddPower(float extraPower)
    {
        power += extraPower;
        power = Mathf.Min(power, maxPower);
        UpdateView();
    }

Well, make sure your scripts on each in their own file and have the same name as the class (files).

Next, be sure to include any errors you might have in the console. Try to fix the errors first before checking if the rest of the code works. Make sure each opening brace ‘{’ has a matching closing one ‘}’ in the right spot. :slight_smile:

You’re missing some braces and healthController in the second script is never declared. It’s never assigned in the first script, either, it seems. If the two scripts are on the same game object, you could use this code on the first:

void Start() {
   healthController = GetComponent<HealthController>();
}

The second script doesn’t need the line with ‘healthController = this’

Have you done some starter tutorials for Unity? https://unity3d.com/learn/beginner-tutorials

Starter tutorials for scripting in Unity: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

Thank you for your reply. The classes are in different files, I just thought it would be easier to overview for you, when its together :slight_smile: The brackets are all closed, I just forgot to put everyone in the thread. I changed the void Start() method but it still doesn’t work

Well, there is no healthController declared in the second class. I can understand wanting to post them together on the forum, sure. However, I think for future readers that might be trying to help, you should update your post so it contains the correct code. That way people won’t have to guess or be confused (like with the braces).

Also, you didn’t say if you have any compilation errors or not.