Forum begins after the advertisement:

 


[part 21] question

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #18395
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    0
    ::
    using UnityEngine;
    using UnityEngine.Scripting.APIUpdating;
    
    public class EnemyMovement : MonoBehaviour
    {
        protected EnemyStats enemy;
        protected Transform player;
    
        protected Vector2 knockbackVelocity;
        protected float knockbackDuartion;
    
        public enum OutOffFrameAction { none, respawnAtEdge, despawn }
        public OutOffFrameAction outOffFrameAction = OutOffFrameAction.respawnAtEdge;
    
        protected bool spawnedOutOfFrame = false;
    
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        protected virtual void Start()
        {
            spawnedOutOfFrame = !SpawnManager.IsWithinBoundaries(transform);
            enemy = GetComponent<EnemyStats>();
    
            //Picks a random player on the screen, instead of alwats picking 1st player
            PlayerMovement[] allPlayers = FindFirstObjectByType<PlayerMovement>();
            player = allPlayers[Random.Range(0, allPlayers.Length)].transform;
        }
    
        // Update is called once per frame
        protected virtual void Update()
        {
            //if enemy currently being knock back, process the knockback
            if (knockbackDuartion > 0)
            {
                transform.position += (Vector3)knockbackVelocity * Time.deltaTime;
                knockbackDuartion -= Time.deltaTime;
            }
            else
            {
                Move();
                HandleOutOfFrameAction();
            }
        }
    
        protected virtual bool HandleOutOfFrameAction()
        {
            //handle enem when it out of frame
            if (!SpawnManager.IsWithinBoundaries(transform))
            {
                switch (outOffFrameAction)
                {
                    case OutOffFrameAction.none:
                    default:
                        break;
                    case OutOffFrameAction.respawnAtEdge:
                        transform.position = SpawnManager.GeneratePosition();
                        break;
                    case OutOffFrameAction.despawn:
                        if (!spawnedOutOfFrame)
                        {
                            Destroy(gameObject);
                        }
                        break;
                }
            }
            else spawnedOutOfFrame = false;
            return false;
        }
    
        public virtual void Knockback(Vector2 velocity, float duration)
        {
            //ignore the knockback if the duration is greater than 0
            if (knockbackDuartion > 0) return;
    
            //begin knockback
            knockbackVelocity = velocity;
            knockbackDuartion = duration;
        }
    
        public virtual void Move()
        {
            //constantly move enemy toward player
            transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemy.currentMoveSpeed * Time.deltaTime);
        }
    }
    

    Cannot implicitly convert type ‘PlayerMovement’ to ‘PlayerMovement[]’

    #18396
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The error suggests using FindObjectsOfType() instead of FindFirstObjectByType(). The method FindObjectsOfType() returns an array of PlayerMovement components, which matches the type declaration PlayerMovement[] allPlayers.

    So the corrected line should be:

    PlayerMovement[] allPlayers = FindObjectsOfType();
    #18397
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    FindObjectsOfType<PlayerMovement>() is deprecated in newer Unity versions so when i change the line above it show this

    ‘Object.FindObjectsOfType<T>()’ is obsolete: ‘Object.FindObjectsOfType has been deprecated. Use Object.FindObjectsByType instead which lets you decide whether you need the results sorted or not. FindObjectsOfType sorts the results by InstanceID but if you do not need this using FindObjectSortMode.None is considerably faster.’

    #18398
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Use the updated method with sorting mode:

    PlayerMovement[] allPlayers = Object.FindObjectsByType(FindObjectsSortMode.None);

    This resolves the deprecation warning by explicitly specifying FindObjectsSortMode.None for better performance.

    #18400
    Kim Il Woo (FGW HCM)
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    fixed

    #18401
    Alp Apustaja
    Level 9
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Great! It seems like the issue with finding player objects in Unity has been resolved. Is there anything specific you would like to discuss or any other programming challenge you’re facing with your Vampire Survivors-style game?

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

Go to Login Page →


Advertisement below: