【问题标题】:How would I check individual lights if my boolean is true constantly如果我的布尔值始终为真,我将如何检查单个灯
【发布时间】:2018-10-26 22:27:11
【问题描述】:

如果 boolean 为真,我需要一种方法来检查所有脚本,然后在此脚本中使用一种方法来查看当前灯的 if 语句中角色站在旁边的 boolean true 才能激活,然后才应该实例化函数被触发。

private bool Once = true;
public Transform Spawnpoint1;
public Transform Spawnpoint2;

public GameObject Prefab1;
public GameObject Prefab2;

//Something like this, but I don't know where to go after that
GameObject[] Lights = GameObject.FindGameObjectsWithTag("lightSwitch");
//foreach(GameObject Light in Lights)

void OnTriggerEnter2D(Collider2D collision)
{
    if (Once == true)
    {
        Debug.Log("It's true");
        if (LightOnOff.isON == true) // It needs to check this constantly
        {
            Debug.Log(" It's Lit");
            Instantiate(Prefab1, Spawnpoint1.position, Spawnpoint1.rotation);

            Instantiate(Prefab2, Spawnpoint2.position, Spawnpoint2.rotation);

            Once = false;
        }
    }
}

这里也是 Light 脚本

public static bool isON;

public void lightOn()
{
    this.GetComponent<Light>().enabled = true;
    isON = true;
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    首先你不应该使用static

    public static bool isON;
    

    个人价值! static 使该值成为“非实例化”值,这意味着它属于整个类而不是实例 => 简单地说,这个变量在所有组件之间共享(有关更多信息,请参阅 this post)。而是使用

    public bool isON;
    

    比像访问组件的实例变量
    更新:从 cmets 我现在知道实际上组件不在对撞机对象上,而是在该脚本附加到的对象的子对象上

    void OnTriggerEnter2D(Collider2D collision)
    {
        // Update. TODO check if the correct Object collided
        //if(!collision.gameObject == <your player>)
    
        if (!Once) return;
    
        // Update this now searches in its own children instead of children of collision
        var lightOnOff = GetComponentInChildren<LightOnOff>(true);
        if(!lightOnOff)
        {
            Debug.Log("No LightOnOff found on collision" + collision.name, this);
            return;
        }
    
        Debug.Log("It's true");
        if (!LightOnOff.isON) return;
    
        Debug.Log(" It's Lit");
        Instantiate(Prefab1, Spawnpoint1.position, Spawnpoint1.rotation);
        Instantiate(Prefab2, Spawnpoint2.position, Spawnpoint2.rotation);
    
        Once = false;
    }
    

    但是,除了使用 LightOnOff 组件,您还可以简单地访问 Light 组件并执行类似
    更新:从 cmets 我现在知道实际上组件不在对撞机对象上而是在此脚本附加到的对象的子对象上

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (!Once) return;
    
        // directly access the Light
        // Update this now searches in its own children instead of children of collision
        var light = GetComponentInChildren<Light>(true);
        if(!light)
        {
            Debug.Log("No Light found on collision" + collision.name, this);
            return;
        }
    
        Debug.Log("It's true");
    
        // Directly check if the Light component is enabled
        if (!light.enabled) return;
    
        Debug.Log(" It's Lit");
        Instantiate(Prefab1, Spawnpoint1.position, Spawnpoint1.rotation);
        Instantiate(Prefab2, Spawnpoint2.position, Spawnpoint2.rotation);
    
        Once = false;
    }
    

    【讨论】:

    • 在层次结构中我必须做些什么,因为我不断收到Debug.Log("No Lightfound on collision" + collision.name, this);
    • @SIGMA 你能再试一次吗.. 我把GetComponent 改为GetComponentInChildren 因为我不知道你的层次结构我假设LightLightOnOff 组件都在一个游戏对象上.这取决于您是使用 Rigidbody 还是仅使用碰撞器,以及您的相应 Light 是否附加到您碰撞的对象或其子对象
    • 如果灯最初不亮,脚本会起作用吗?因为玩家必须打开灯,这个脚本是否必须放在灯上才能工作。
    • GetComponentInChildren:它有一个过载,你可以决定同时获取当前禁用的组件。并且脚本必须位于带有ColliderRigidBody 的对象上(以及子对象中的对撞机)。 Light 组件应位于在其Collider 组件中设置了isTrigger 标志的对象上
    • 这是我的层次结构的图片,它仍然给出错误gyazo.com/dd0c9a11ee8dc5c8a727cd05cf9cb1d2
    【解决方案2】:

    您无需为此跟踪所有灯光。

    你需要改变的是你应该让isON 不是静态的。这是因为实际的灯可能亮或不亮,而不是灯的概念是否亮。

    public bool isON;
    

    检查 Collider2D 是否与您正在碰撞的关联对象,并在那里寻找灯光。以下代码假定任何灯光都将位于与触发器或其子之一相同的 GameObject 上。

    void OnCollisionEnter2D(Collider2D col) {
        // only activate once
        if (once) {
            // Get light if exists
            GameObject collidedObject = col.gameObject;
            Light light = collidedObject.GetComponentInChildren<Light>();
    
            if (light != null) {
                // We have a light, check if it's on. We only care about the collided light
                if (light.isON) {
                    Debug.Log("It's Lit fam");
    
                    Instantiate(Prefab1, Spawnpoint1.position, Spawnpoint1.rotation);
                    Instantiate(Prefab2, Spawnpoint2.position, Spawnpoint2.rotation);
    
                    // Note that if we run into another lit light nothing will happen, 
                    // even if its the first time running into that particular light
                    Once = false;
                }
            }
        }
    }
    

    另外,您可以只使用if(something) 而不是if(something == true)

    【讨论】:

    • Light 没有成员 isOn ;)
    • GetComponentInChildren 是做什么的
    • GetComponent 相同,但它会检查与触发器相同的游戏对象以及其所有子对象的Light
    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2013-11-16
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多