Z
Z
zxcurffed2021-09-08 09:45:41
C++ / C#
zxcurffed, 2021-09-08 09:45:41

How to fix a bug with an enemy and a bullet?

script error
Assets\Scripts\Enemy.cs(39,56): error CS1061: 'Enemy' does not contain a definition for 'ChangeHealth' and no accessible extension method 'ChangeHealth' accepting a first argument of type 'Enemy' could be found (are you missing a using directive or an assembly reference?) THIS ERROR

CODE BULLET END ENEMY

THIS ENEMY

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

public class Enemy : MonoBehaviour
{
private float timeBtwAttack;
public float startTimeBtwAttack;

public int health;
public float speed;
public GameObject deathEffect;
public int damage;
private float stopTime;
public float startStopTime;
public float normalSpeed;
private player player;
private Animator anim;
public int rotationOffset = -90;
private float rotZ;

private void Start()
{
anim = GetComponent();
player = FindObjectOfType();
}

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().TakeDamage(damage);
}
if (hitInfo.collider.CompareTag("Player") && enemyBullet)
{
hitInfo.collider.GetComponent().ChangeHealth(-damage);
}
DestroyBullet();
}

if (stopTime <= 0)
{
speed = normalSpeed;
}
else
{
speed = 0;
stopTime -= Time.deltaTime;
}
if (health <= 0)
{
Destroy(gameObject);
}
if (player.transform.position.x > transform.position.x)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
}

transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
}

public void TakeDamage(int damage)
{
stopTime = startStopTime;
Instantiate(deathEffect, transform.position, Quaternion.identity);
health = damage;
}
public void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
if (timeBtwAttack <= 0)
{
OnEnemyAttack();
anim SetTrigger("attack");
}
else
{
timeBtwAttack -= Time.deltaTime;
}
}
}
public void OnEnemyAttack()
{
Instantiate(deathEffect, player.transform.position, Quaternion.identity);
player.health -= damage;
timeBtwAttack = startTimeBtwAttack;
}
}

THIS BULLET

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

public class Bullet : MonoBehavior {

public float speed;
public float lifeTime;
public float distance;
public int damage;
public LayerMask whatIsSolid;

[SerializeField] bool enemyBullet;

public GameObject destroyEffect;

private void Start()
{
Invoke("DestroyBullet", lifeTime);
}

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().TakeDamage(damage);
}
if (hitInfo.collider.CompareTag("Player") && enemyBullet)
{
hitInfo.collider.GetComponent().ChangeHealth(-damage);
}
DestroyBullet();
}

transform.Translate(Vector2.up * speed * Time.deltaTime);
}

void DestroyBullet() {
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}

help please, for 2 weeks I have been fiddling with this question

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question