Forum begins after the advertisement:
[part 21] question
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [part 21] question
- This topic has 2 replies, 2 voices, and was last updated 2 weeks ago by
Alp Apustaja.
-
AuthorPosts
-
June 27, 2025 at 11:26 am #18395::
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[]’
June 27, 2025 at 11:35 am #18396::The error suggests using
FindObjectsOfType()
instead ofFindFirstObjectByType()
. The methodFindObjectsOfType()
returns an array of PlayerMovement components, which matches the type declarationPlayerMovement[] allPlayers
.So the corrected line should be:
PlayerMovement[] allPlayers = FindObjectsOfType();
June 27, 2025 at 11:42 am #18397::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.’
June 27, 2025 at 11:45 am #18398::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.June 27, 2025 at 12:53 pm #18400June 27, 2025 at 12:55 pm #18401::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?
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: