You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中GameObject.FindGameObjectsWithTag无法识别克隆对象致移动失效

Alright, let's figure out why those instantiated circles aren't moving—here are the most likely issues and fixes based on your code:

1. Make sure your circle prefab has the "Enemy" tag

Your enemy movement script uses GameObject.FindGameObjectsWithTag("Enemy") to target objects to move. If your circle prefab isn't tagged Enemy, the script will never find the instantiated circles, so they'll never get movement logic applied.

  • Fix: Select your circle prefab, use the tag dropdown at the top of the Inspector to choose "Enemy" (create the tag first if it doesn't exist).

2. Ensure the circle prefab has a Rigidbody2D component

Your movement code relies on Rigidbody2D to set velocity. If the prefab doesn't have this component, obj.GetComponent<Rigidbody2D>() will return null (you can check your Debug logs to confirm this—look for null values printed from the Debug.Log(rb) line).

  • Fix: Add a Rigidbody2D component to your circle prefab. For most 2D enemies, you'll want to set Gravity Scale to 0 so they don't fall unless you intend them to.

3. Double-check the player variable is assigned

Your movement script uses player.position to calculate direction, but if the player variable isn't linked to your actual player GameObject in the Inspector, it'll be null. This makes the direction vector zero, so rb.velocity will stay 0.

  • Fix: Select the GameObject with your enemy control script, find the player field in the Inspector, and drag your player GameObject into it.

4. Fix the Z-axis position when instantiating

Your spawn code uses Camera.main.farClipPlane / 2 for the Z coordinate. In 2D games (usually using an orthographic camera), objects should stay on Z=0 (or match your player's Z position) to be in the same plane. A wrong Z value can make enemies appear off-screen or break the direction calculation.

  • Fix: Update your spawn code to use Z=0:
screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 0));

5. Confirm the enemy control script is active

Make sure the GameObject holding your enemy control script is enabled, and the script itself isn't disabled in the Inspector. If it's inactive, FixedUpdate won't run at all.

Bonus: Optimize your movement code

Calling GameObject.FindGameObjectsWithTag every FixedUpdate is inefficient, especially as you add more enemies. A better approach is to attach a movement script directly to the circle prefab so each enemy handles its own movement:

public class EnemyMovement : MonoBehaviour
{
    public float moveSpeed;
    private Rigidbody2D rb;
    private Transform player;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        // Grab the player once at start (assuming your player has a "Player" tag)
        player = GameObject.FindWithTag("Player").transform;
    }

    void FixedUpdate()
    {
        if (player == null) return; // Exit if player is missing

        Vector2 direction = (player.position - transform.position).normalized;
        rb.velocity = direction * moveSpeed;
    }
}

This way, you don't have to search for enemies every frame, and each instantiated circle will start moving automatically.

内容的提问来源于stack exchange,提问作者Stellan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:42:42