0

I need a way to check all scripts if boolean is true and then a way in this script to see in the if statement of the current light the character is standing next to has the boolean true to activate and only then should the Instantiate function be triggered.

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

here is the Light script as well

public static bool isON;

public void lightOn()
{
    this.GetComponent<Light>().enabled = true;
    isON = true;
}
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
SIGMA
  • 61
  • 1
  • 7

2 Answers2

1

You don't need to keep track of all the lights just for that.

Something you do need to change is that you should make isON not static. This is because an actual light might be on or not, not that the concept of lights are on or not.

public bool isON;

Check the Collider2D for the associated object you're colliding with, and look for a light there. The following code assumes any light will be on the same GameObject as a Trigger or one of its children.

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

Also, you can just use if(something) instead of if(something == true)

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
1

First of all you shouldn't use static

public static bool isON;

for an individual value! static makes the value a "non instanced" value, meaning it belongs to the entire class instead of instances => To say it in simple words this variable is shared between all your components (see this post for more information). Instead use

public bool isON;

Than access the insteance variable of the component like
Update: From the comments I now know that actually the components are not on the collider object but rather on a child of the object this script is attached to

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

But instead of using your LightOnOff component you could also simply acces the Light component and do something like
Update: From the comments I now know that actually the components are not on the collider object but rather on a child of the object this script is attached to

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;
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Is there something I have to do in the hierarchy because I keep getting `Debug.Log("No Lightfound on collision" + collision.name, this);` – SIGMA Oct 27 '18 at 05:39
  • @SIGMA could you try it again .. I changed `GetComponent` to `GetComponentInChildren` since I don't know your hierachy I asumed the `Light` and `LightOnOff` components are all on one GameObject. This depends if your are using `Rigidbody` or only colliders and if your according Light is attached to the object you collide with or its children – derHugo Oct 27 '18 at 05:41
  • will the script work if the light is initially not on? because the player has to turn on the light and does this script have to be put on the light to work. – SIGMA Oct 29 '18 at 07:43
  • See [GetComponentInChildren](https://docs.unity3d.com/ScriptReference/GameObject.GetComponentInChildren.html): it has an overload taking a bool where youc an decide to also get currently disabled components. And the script has to be on the Object with a `Collider` or a `RigidBody` on it (and a collider in the children). The `Light` component should be on the object with the `isTrigger` flag set in its `Collider` component – derHugo Oct 29 '18 at 07:53
  • Here is a picture of my hierarchy and it's still giving that error https://gyazo.com/dd0c9a11ee8dc5c8a727cd05cf9cb1d2 – SIGMA Oct 29 '18 at 09:03
  • so from your ouput I see that the collider object is "Axel" => you don't have the Light on the triger at all but rather want it the other way round? In which class and on which GameObject is the `OnTriggerEnter2D`? – derHugo Oct 29 '18 at 09:37
  • I updated my answer to use the hierachy setup you told me now. Make sure to somehow check if the collision is coming from the correct GameObject – derHugo Oct 29 '18 at 13:06