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:
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);
}
}
}
CodePudding user response:
Your code is trying to reference CharacterController
, but the code you linked above that states the type is called PlayerController
.