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

Unity中Light组件报错“‘Light’不包含‘enable’定义”的问题求助

Unity脚本错误:Light类型找不到enable定义的解决方法

Hey there, let's fix this error you're facing right away!

First, let's recap your original code and the error you encountered:

原脚本代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script : MonoBehaviour {
    private Light m_light;
    void Start() {
        m_light = GetComponent<Light>();
    }
    void Update() {
        if (Input.GetKeyUp(KeyCode.Space)) {
            m_light.enable = !m_light.enable;
        }
    }
}

错误提示

“‘Light’类型不包含‘enable’的定义,也找不到可接受‘Light’类型作为第一个参数的可访问扩展方法‘enable’(是否缺少using指令或程序集引用?)”


问题原因&解决方法

This is just a simple typo! In Unity, the property that controls whether a component is active or not is enabled (notice the extra 'd' at the end), not enable. This is a standard property across almost all Unity components, so it's an easy mistake to make.

Just correct that single property name, and your script will work as expected. Here's the fixed code:

修正后的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script : MonoBehaviour {
    private Light m_light;
    void Start() {
        m_light = GetComponent<Light>();
    }
    void Update() {
        if (Input.GetKeyUp(KeyCode.Space)) {
            // 把enable改成enabled即可
            m_light.enabled = !m_light.enabled;
        }
    }
}

额外小提示

If you ever want to toggle the entire GameObject (not just the Light component), you'd use gameObject.SetActive(!gameObject.activeSelf) instead—but for your use case of turning the light on/off, enabled is exactly what you need.

内容的提问来源于stack exchange,提问作者Максим Циганський

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 18:47:54