Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Health Pickup System in Unity doesnt work and i will get an Error

I make a script that detects if a HealthPickup has been triggered. If this is the case, it should check whether a certain script is present. If this is the case too, a function within this script should be accessed. But I get this error in the console:

‘CharacterController’ does not contain a definition for ‘ChangeHealth’
and no accessible extension method ‘ChangeHealth’ accepting a first
argument of type ‘CharacterController’ could be found (are you missing
a using directive or an assembly reference?)

My codes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CharacterController.cs

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

public class PlayerController : MonoBehaviour
{
Rigidbody2D rigidbody2d;
float horizontalmovement;
float verticalmovement;

//Playerstats Variabeln
int currentHealth;
int maxHealth = 5;

public float moveSpeed = 3.0f;

public void Start()
{
    rigidbody2d = GetComponent<Rigidbody2D>();
    currentHealth = 1;

}

public void Update()
{
    horizontalmovement = Input.GetAxis("Horizontal");
    verticalmovement = Input.GetAxis("Vertical");
}

private void FixedUpdate()
{
    Vector2 position = rigidbody2d.position;

    position.x = position.x + moveSpeed * horizontalmovement * Time.deltaTime;
    position.y = position.y + moveSpeed * verticalmovement * Time.deltaTime;

    rigidbody2d.MovePosition(position);
}

public void ChangeHealth(int amount)
{
    currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    Debug.Log(currentHealth + "/" + maxHealth);
}
 }

GetHealth.cs

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

public class GetHealth : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other)
    {
        CharacterController controller = other.GetComponent<CharacterController>();

        if(controller != null)
        {
            controller.ChangeHealth(1);
            Destroy(gameObject);
        }
    }
}

>Solution :

Your code is trying to reference CharacterController, but the code you linked above that states the type is called PlayerController.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading