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

Unity太空侵略者游戏开发报错:Keyword 'this'不在当前上下文

问题解决:Unity太空侵略者游戏中的this上下文错误与Evaluate方法错误

问题概述

刚接触Unity,跟着教程开发太空侵略者游戏,已完成敌人实例化,但代码中使用this时触发大量Keyword 'this' is not available in the current context错误,同时存在float类型无Evaluate方法的报错。

错误原因分析

1. this关键字的使用限制

在C#中,类的字段初始化阶段不能使用this——字段初始化逻辑在类实例构造前执行,此时this尚未指向有效的对象实例。你直接用this.rows、this.amountKilled初始化totalInvaders和percentKilled字段,违反了这个规则。

2. Evaluate方法调用错误

speed被定义为float类型,但float本身没有Evaluate方法。教程中大概率是将speed设计为AnimationCurve类型(用于根据击杀百分比动态调整速度),你误写成了基础数值类型。

修正后的代码

using UnityEngine;

public class Invaders : MonoBehaviour
{
    public Invader[] prefabs;
    
    public int rows = 4;
    public int columns = 7;

    // 将float改为AnimationCurve,实现速度随击杀进度动态变化
    public AnimationCurve speedCurve;

    public int amountKilled { get; private set; }
    public int totalInvaders { get; private set; }

    // 改为计算属性,实时计算击杀百分比
    public float percentKilled => (float)amountKilled / totalInvaders;

    private Vector3 _direction = Vector2.right;

    private void Awake() 
    {
        // 在Awake中初始化总敌人数,此时实例已创建,可正常访问rows和columns
        totalInvaders = rows * columns;
        
        for (int row = 0; row < rows; row++)
        {
            float width = 1.0f * (columns - 1);
            float height = -0.5f * (rows - 1);
            Vector2 centering = new Vector2(-width / 2, -height / 2);
            Vector3 rowPosition = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f);

            for (int col = 0; col < columns; col++)
            {
                Invader invader = Instantiate(prefabs[row], transform);
                invader.killed += InvaderKilled;
                Vector3 position = rowPosition;
                position.x += col * 1.0f;
                invader.transform.position = position;
            }
        }
    }

    private void Update()
    {
        // 通过AnimationCurve的Evaluate方法获取当前速度
        float currentSpeed = speedCurve.Evaluate(percentKilled);
        transform.position += _direction * currentSpeed * Time.deltaTime;

        Vector3 leftEdge = Camera.main.ViewportToWorldPoint(Vector3.zero);
        Vector3 rightEdge = Camera.main.ViewportToWorldPoint(Vector3.right);

        foreach (Transform invader in transform)
        {
            if (!invader.gameObject.activeInHierarchy) 
            {
                continue;
            }

            if (_direction == Vector3.right && invader.position.x > rightEdge.x) 
            {
                AdvanceRow();
            }   
            else if (_direction == Vector3.left && invader.position.x < leftEdge.x)
            {
                AdvanceRow();
            }
        }
    }

    private void AdvanceRow()
    {
        _direction.x *= -1.0f;

        Vector3 position = transform.position;
        position.y -= 1.0f;
        transform.position = position;
    }

    private void InvaderKilled()
    {
        amountKilled++;
        // 可在此添加击杀后的逻辑,如播放音效、特效等
    }
}

额外说明

  • 若不需要动态速度,只想用固定值,可将speedCurve改回float speed = 0.5f,并把Update中的速度计算代码改为transform.position += _direction * speed * Time.deltaTime;。
  • 代码中移除了冗余的this——类成员方法中访问自身字段/属性时,可省略this让代码更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 09:35:35