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

Godot报错“Invalid get index 'transform' (on base: 'NIL')”求助

Godot脚本Camera3D引用失败导致NIL错误

我跟着教程编写Godot脚本,完全照搬教程内容,但运行项目时弹出**"Invalid get index 'transform' (on base: 'NIL')"**错误,排查无果。删除所有和CAMERA_CONTROLLER相关的代码后项目可正常运行,说明脚本无法识别对应的Camera3D节点。我已多次重写脚本、重置Camera3D位置,问题仍未解决。

场景树结构:

  • CharacterBody3D(挂载当前脚本)
    • MeshInstance3D
    • CollisionShape3D
    • Camera3D(需要被脚本引用的节点)

相关脚本代码:

extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5

@export var TILT_LOWER_LIMIT := deg_to_rad(-90.0)
@export var TILT_UPPER_LIMIT := deg_to_rad(90.0)
@export var CAMERA_CONTROLLER : Camera3D

var _mouse_input : bool = false
var _mouse_rotation : Vector3
var _rotation_input : float
var _tilt_input : float

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _input(event):
    if event.is_action_pressed("exit"):
        get_tree().quit()
        
func _unhandled_input(event):
    _mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
    if _mouse_input:
        _rotation_input = -event.relative.x
        _tilt_input = -event.relative.y
        print(Vector2(_rotation_input,_tilt_input,))
        
func _update_camera(delta):
    
    # Rotate camera using euler rotation
    _mouse_rotation.x += _tilt_input * delta
    _mouse_rotation.x = clamp(_mouse_rotation.x, TILT_LOWER_LIMIT, TILT_UPPER_LIMIT)
    _mouse_rotation.y += _rotation_input * delta
    
    CAMERA_CONTROLLER.transform.basis = Basis.from_euler(_mouse_rotation)
    CAMERA_CONTROLLER.rotation.z = 0.0
    
    _rotation_input = 0.0
    _tilt_input = 0.0
        
func _ready():
    Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y -= gravity * delta
        
    _update_camera(delta)

    # Handle Jump.
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    if direction:
        velocity.x = direction.x * SPEED
        velocity.z = direction.z * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        velocity.z = move_toward(velocity.z, 0, SPEED)

    move_and_slide()

排查与解决方法

  • 手动赋值导出变量:选中挂载脚本的CharacterBody3D节点,在检查器面板找到CAMERA_CONTROLLER变量,将场景中的Camera3D节点拖入该变量的赋值框,确保引用关系正确。
  • 改用节点路径自动获取:把@export var CAMERA_CONTROLLER : Camera3D替换为onready var CAMERA_CONTROLLER := $Camera3D(注意替换成你场景中Camera3D的实际名称),脚本会在_ready时自动查找子节点,避免手动赋值遗漏。
  • 核对节点名称:确认场景中Camera3D的节点名称和脚本中的引用完全一致,Godot对节点名称大小写敏感。
  • 检查脚本挂载位置:确保脚本挂载在CharacterBody3D节点上,而非其他节点,否则节点查找的上下文会出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 02:15:36