Forum begins after the advertisement:
Bat Enemy Issue
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › Bat Enemy Issue
- This topic has 8 replies, 2 voices, and was last updated 1 month, 2 weeks ago by Ferral Child.
-
AuthorPosts
-
October 31, 2024 at 12:36 pm #16211::
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>
October 31, 2024 at 2:18 pm #16212November 1, 2024 at 4:52 am #16216::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>
November 1, 2024 at 9:02 am #16217November 1, 2024 at 2:16 pm #16218::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>
November 1, 2024 at 2:32 pm #16219November 1, 2024 at 3:19 pm #16220::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>
November 1, 2024 at 11:00 pm #16222::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
- There is no stunduration variable to refer to in your honey script
sr.flipx
should besr.flipX
playerController
should bePlayerController
- In
UpdateEnemyStates()
, EnemyState is singular instead of plural EnemyStates
and 2 other people have upvoted this post. November 2, 2024 at 3:45 am #16227::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!
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: