繁体   English   中英

如果我的布尔值始终为真,我将如何检查单个灯

[英]How would I check individual lights if my boolean is true constantly

我需要一种方法来检查所有脚本,如果布尔值为true,然后在此脚本中使用一种方法来查看当前光的if语句旁边的字符是否具有布尔true才能激活,则只有在此时才触发Instantiate函数。

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;
}

您无需为此仅跟踪所有照明。

您确实需要更改的地方是应该使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)

首先,您不应该使用static

public static bool isON;

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

public bool isON;

比访问组件的实例变量
更新:从注释中,我现在知道组件实际上不在碰撞对象上,而是在此脚本附加到的对象的子对象上

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使用LightOnOff组件之外,您还可以简单地访问Light组件并执行类似的操作
更新:从注释中,我现在知道组件实际上不在碰撞对象上,而是在此脚本附加到的对象的子对象上

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM