Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 手电筒消失了,但没有';不要再出现了_C#_Unity3d - Fatal编程技术网

C# 手电筒消失了,但没有';不要再出现了

C# 手电筒消失了,但没有';不要再出现了,c#,unity3d,C#,Unity3d,我想给我的游戏加一个手电筒。当我按下按钮使其消失时,它消失了,但当我按下按钮使其再次出现时,它没有消失。在过去的一个半小时里,我一直在努力寻找解决方案,也查看了文档,但什么都没有找到 using System.Collections.Generic; using UnityEngine; public class flashlight : MonoBehaviour { // Start is called before the first frame update void

我想给我的游戏加一个手电筒。当我按下按钮使其消失时,它消失了,但当我按下按钮使其再次出现时,它没有消失。在过去的一个半小时里,我一直在努力寻找解决方案,也查看了文档,但什么都没有找到

using System.Collections.Generic;
using UnityEngine;

public class flashlight : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            gameObject.SetActive(true);
        }

        if (Input.GetKeyUp(KeyCode.Alpha2))
        {
            gameObject.SetActive(false);
        }
    }
}

脚本将与
游戏对象一起停用,因此它不会侦听
输入.GetKeyUp(KeyCode.Alpha2)

为了避免这种情况,请创建一个空的游戏对象来保存脚本,并使灯光成为所述游戏对象的子对象,然后当您停用灯光时,脚本仍处于活动状态,并侦听
输入

像这样更新脚本以指定灯光子对象


非常感谢你!你帮我省了很多时间。
public class flashlight : MonoBehaviour
{
    public GameObject light;//Assign this is the inspector

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            light.SetActive(true);
        }
        if (Input.GetKeyUp(KeyCode.Alpha2))
        {
            light.SetActive(false);
        }
    }
}