Forum begins after the advertisement:


Bat Enemy Issue

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16211
    Ferral Child
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hello,

    Whenever I try to run the game I get the error message “Assets\Honey.cs(57,22): error CS0115: ‘Honey.EnemyGetsHit(float, Vector2, float)’: no suitable method found to override”

    (Honey is what I called Bat)

    Here is my code. Any help would be greatly appreciated.

    <code>public class Honey : Enemy
    {
        [SerializeField] private float chaseDistance;
    
        float timer;
    
        // Start is called before the first frame update
       protected override void Start()
        {
            base.Start();
            ChangeState(EnemyStates.Honey_Idle);
        }
    
        // Update is called once per frame
        protected override void UpdateEnemyStates()
        {
            float _dist = Vector2.Distance(transform.position, PlayerController.Instance.transform.position);
    
            switch (currentEnemyState)
            {
                case EnemyStates.Honey_Idle:
                    if(_dist < chaseDistance)
                    {
                        ChangeState(EnemyState.Honey_Chase);
                    }
                    break;
    
                case EnemyStates.Honey_Chase:
                   rb.MovePosition(Vector2.MoveTowards(transform.position, PlayerController.Instance.transform.position, Time.deltaTime * speed));
    
                    FlipHoney();
                    break;
    
                case EnemyStates.Honey_Stunned:
    
                    timer += Time.deltaTime;
    
                    if (timer > stunDuration)
                    {
                        ChangeState(EnemyStates.Honey_Idle);
                        timer = 0;
                    }
    
                    break;
    
                case EnemyStates.Honey_Death:
                    break;
            }
        }
    
    public override void EnemyGetsHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            base.EnemyGetsHit(_damageDone, _hitDirection, _hitForce);
    
            if(health > 0)
            {
                ChangeState(EnemyStates.Honey_Stunned);
            }
            else
            {
                ChangeState(EnemyStates.Honey_Death);
            }
        }
    
        void FlipHoney()
        {
            if(PlayerController.Instance.transform.position.x < transform.position.x)
            {
                sr.flipx = true;
            }
            else
            {
                sr.flipx = false;
            }
        }
    }</code>
    #16212
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    1
    ::

    Hi, could you send your enemy script as well?

    has upvoted this post.
    #16216
    Ferral Child
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Yep!

    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
    
        [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
        [SerializeField] protected float speed;
        [SerializeField] protected float damage;
    
        protected float recoilTimer;
        protected Rigidbody2D rb;
        protected SpriteRenderer sr;
    
        protected enum EnemyStates
        {
            Bug_Idle,
            Bug_Flip,
    
            Honey_Idle,
            Honey_Chase,
            Honey_Stunned,
            Honey_Death,
    
        }
        protected EnemyStates currentEnemyState;
    
        // Start is called before the first frame update
        protected virtual void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            sr = GetComponent<SpriteRenderer>();
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
    
            if (isRecoiling)
            {
                if (recoilTimer < recoilLength)
                {
                    recoilTimer += Time.deltaTime;
                }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0;
                }
            }
            else
            {
                UpdateEnemyStates();
            }
        }
    
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _damageDone;
            if (!isRecoiling)
            {
                rb.velocity = _hitForce * recoilFactor * _hitDirection;
                isRecoiling = true;
            }
        }
    
        protected void OnTriggerStay2D(Collider2D _other)
        {
            if (_other.CompareTag("Player") && !playerController.Instance.pState.invincible)
            {
                Attack();
                playerController.Instance.HitStopTime(0, 5, 0.1f);
            }
        }
    
        protected virtual void UpdateEnemyStates()
        {
    
        }
        protected void ChangeState(EnemyStates _newState)
        {
            currentEnemyState = _newState;
        }
    
        protected virtual void Attack()
        {
            playerController.Instance.TakeDamage(damage);
        }
    
    }</code>
    #16217
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    1
    ::

    Ok, your EnemyGetsHit() in the Honey script is supposed to override the EnemyHit() in the Enemy script, so they should have the same naming convention for it to override properly So renaming either of them for both names to match might be the solution

    has upvoted this post.
    #16218
    Ferral Child
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    I changed it from EnemyGetsHit to EnemyHit in Honey and it made more errors show up?

    <code>Assets\Honey.cs(23,60): error CS0103: The name 'PlayerController' does not exist in the current context
    Assets\Honey.cs(30,33): error CS0103: The name 'EnemyState' does not exist in the current context
    Assets\Honey.cs(35,72): error CS0103: The name 'PlayerController' does not exist in the current context
    Assets\Honey.cs(44,29): error CS0103: The name 'stunDuration' does not exist in the current context
    Assets\Honey.cs(73,12): error CS0103: The name 'PlayerController' does not exist in the current context
    Assets\Honey.cs(75,16): error CS1061: 'SpriteRenderer' does not contain a definition for 'flipx' and no accessible extension method 'flipx' accepting a first argument of type 'SpriteRenderer' could be found (are you missing a using directive or an assembly reference?)
    Assets\Honey.cs(79,16): error CS1061: 'SpriteRenderer' does not contain a definition for 'flipx' and no accessible extension method 'flipx' accepting a first argument of type 'SpriteRenderer' could be found (are you missing a using directive or an assembly reference?)</code>
    #16219
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    1
    ::

    oh, I didnt see that you used base.EnemyGetsHit(_damageDone, _hitDirection, _hitForce); in your EnemyHit function in your honey script, so you can change it to base.EnemyHit(_damageDone, _hitDirection, _hitForce); instead

    has upvoted this post.
    #16220
    Ferral Child
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    I changed it when you originally pointed out the issue. But that’s when all of the other errors showed up. I’ll put my updated Honey and Enemy Script down below.

    <code>using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Security.AccessControl;
    using UnityEngine;
    
    public class Honey : Enemy
    {
        [SerializeField] private float chaseDistance;
    
        float timer;
    
        // Start is called before the first frame update
       protected override void Start()
        {
            base.Start();
            ChangeState(EnemyStates.Honey_Idle);
        }
    
        // Update is called once per frame
        protected override void UpdateEnemyStates()
        {
            float _dist = Vector2.Distance(transform.position, PlayerController.Instance.transform.position);
    
            switch (currentEnemyState)
            {
                case EnemyStates.Honey_Idle:
                    if(_dist < chaseDistance)
                    {
                        ChangeState(EnemyState.Honey_Chase);
                    }
                    break;
    
                case EnemyStates.Honey_Chase:
                   rb.MovePosition(Vector2.MoveTowards(transform.position, PlayerController.Instance.transform.position, Time.deltaTime * speed));
    
                    FlipHoney();
                    break;
    
                case EnemyStates.Honey_Stunned:
    
                    timer += Time.deltaTime;
    
                    if (timer > stunDuration)
                    {
                        ChangeState(EnemyStates.Honey_Idle);
                        timer = 0;
                    }
    
                    break;
    
                case EnemyStates.Honey_Death:
                    break;
            }
        }
    
    public override void EnemyHit( float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            base.EnemyHit(_damageDone, _hitDirection, _hitForce);
    
            if (health > 0)
            {
                ChangeState(EnemyStates.Honey_Stunned);
            }
            else
            {
                ChangeState(EnemyStates.Honey_Death);
            }
        }
    
        void FlipHoney()
        {
            if(PlayerController.Instance.transform.position.x < transform.position.x)
            {
                sr.flipx = true;
            }
            else
            {
                sr.flipx = false;
            }
        }
    }</code>
    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
    
        [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
        [SerializeField] protected float speed;
        [SerializeField] protected float damage;
    
        protected float recoilTimer;
        protected Rigidbody2D rb;
        protected SpriteRenderer sr;
    
        protected enum EnemyStates
        {
            Bug_Idle,
            Bug_Flip,
    
            Honey_Idle,
            Honey_Chase,
            Honey_Stunned,
            Honey_Death,
    
        }
        protected EnemyStates currentEnemyState;
    
        // Start is called before the first frame update
        protected virtual void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            sr = GetComponent<SpriteRenderer>();
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
    
            if (isRecoiling)
            {
                if (recoilTimer < recoilLength)
                {
                    recoilTimer += Time.deltaTime;
                }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0;
                }
            }
            else
            {
                UpdateEnemyStates();
            }
        }
    
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _damageDone;
            if (!isRecoiling)
            {
                rb.velocity = _hitForce * recoilFactor * _hitDirection;
                isRecoiling = true;
            }
        }
    
        protected void OnTriggerStay2D(Collider2D _other)
        {
            if (_other.CompareTag("Player") && !playerController.Instance.pState.invincible)
            {
                Attack();
                playerController.Instance.HitStopTime(0, 5, 0.1f);
            }
        }
    
        protected virtual void UpdateEnemyStates()
        {
    
        }
        protected void ChangeState(EnemyStates _newState)
        {
            currentEnemyState = _newState;
        }
    
        protected virtual void Attack()
        {
            playerController.Instance.TakeDamage(damage);
        }
    
    }</code>
    #16222
    Chloe Lim
    Level 10
    Moderator
    Helpful?
    Up
    3
    ::

    I think you need to add IntelliSense into your visual studio as these errors are caused by naming convention errors, which can be underlined with the help of intellisense

    1. There is no stunduration variable to refer to in your honey script
    2. sr.flipx should be sr.flipX
    3. playerController should be PlayerController
    4. In UpdateEnemyStates(), EnemyState is singular instead of plural EnemyStates
    and 2 other people have upvoted this post.
    #16227
    Ferral Child
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    I added IntelliSense and it made this so much easier to fix! I was able to find all of the errors and fix them in the script. Thank you so much for the help, it is greatly appreciated!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: