Помогите! ОшибкаCS1061
Ошибка CS1061 "Player" не содержит определения "ChangeHealth", и не удалось найти доступный метод расширения "ChangeHealth", принимающий тип "Player" в качестве первого аргумента (возможно, пропущена директива using или ссылка на сборку). Player
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public ControlType controlType;
public Joystick joystick;
public float speed;
public float health;
public enum ControlType{PC, Android}
private Rigidbody2D rb;
private Vector2 moveInput;
private Vector2 moveVelocity;
private Animator anim;
private bool facingRight = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
if(controlType == ControlType.PC)
{
joystick.gameObject.SetActive(false);
}
}
void Update()
{
if (controlType == ControlType.PC)
{
moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
else if (controlType == ControlType.Android)
{
moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
}
moveVelocity = moveInput.normalized * speed;
if (moveInput.x == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if(!facingRight && moveInput.x > 0)
{
Flip();
}
else if(facingRight && moveInput.x < 0)
{
Flip() ;
}
if(health <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
public float lifetime;
public float distance;
public int damage;
public LayerMask whatIsSolid;
[SerializeField] bool enemyBullet;
private void Update()
{
RaycastHit2D HitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if(HitInfo.collider != null)
{
if (HitInfo.collider.CompareTag("Enemy"))
{
HitInfo.collider.GetComponent<Enemy>().TakeDamage(damage);
}
if (HitInfo.collider.CompareTag("Player"))
{
HitInfo.collider.GetComponent<Player>().ChangeHealth(damage);
}
Destroy(gameObject);
}
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
}
Ответы (1 шт):
Автор решения: iKuzmychov
→ Ссылка
Исходя из вашего кода, класс Player содержит 4 метода:
private void Start()
private void Update()
private void FixedUpdate()
private void Flip()
В классе Bullet в строчке
HitInfo.collider.GetComponent<Player>().ChangeHealth(damage);
вы пытаетесь вызвать метод ChangeHealth(int) у объекта типа Player, но такого метода просто нет. Вероятно, стоит его создать. Или хотя бы написать:
HitInfo.collider.GetComponent<Player>().health -= damage;