Unsure how to receive OnTriggerEnter from a different object.

Hi!

What I want to do:

Have an object, called “PressurePlate” broadcast its OnTriggerEnter status to another script.

This “other” script is the main script for the scene and allows the entire scene to function.
The Main Script is attached to a random game object, and the pressure plate has no script, and I need to keep it that way.

I will use a solution to this problem later on in the development of this script as well.

What I have now:

IEnumerator IntroTutorialTextsAndEnemies2()
    {
        Debug.Log("CoroutinePart2Start!");
        PressurePlate.gameObject.GetComponent(OnTriggerEnter);
    }

What I know I need to do on the Other Script:

public void OnTriggerEnter(Collider other)
    {
        PressurePlateOn = true;
    }

Reminder of what I want to do:

Object 1 (Ontriggerenter) sends a message to Script 1 (the main script). I need to make this able to be referenced in a line of code, like this:

I want to do something like this, but only if the pressure plate has been pressed.

Debug.Log("CoroutineStarted!");
        //PRESSUREPLATE_ONTRIGGERENTER=TRUE//
        Intro1.SetActive(true);
        yield return new WaitForSeconds(10);
        Intro1.SetActive(false);
        Intro2.SetActive(true);
        yield return new WaitForSeconds(10);
        Intro2.SetActive(false);
        PressurePlate.SetActive(true);
        Debug.Log("CoroutinePart1Over!");

Please Help… I need it soon as well! Have a good day!

I might have found something.

 void OnTriggerEnter2D(Collider2D collider){
    
       if (collider.name == "Wall") {
         //Do something with Wall or your character
                
       } else {
    
             if (collider.name == "Floor") {
            //Do something with floor or your character               
                    
       }

This was in a script from unity answers (https://answers.unity.com/questions/681599/index.html)

Still unsure how to implement anything…

I might have just found a way to explain it better. If I was confusing, please excuse me, I have Autism and Dyspraxia, and sometimes make mistakes (like a human)…

I have an empty gameobject. On this GameObject, there is a script.

I have a target.

If OnTriggerEnter = true on the target, I need to send that state of being true to the GameObject Script.

I don’t know how to do that.

If you want to get an OnTriggerEnter call from the pressure plate, it has to have a script on it. OnTriggerEnter is sent only to scripts on the GameObject they’re attached to.

You can add a script to the pressure plate at runtime, if you want to avoid having it there at edit time?

Hi @Baste ,

I understand the first bit of what you are saying, however, I do not understand the runtime/edit stuff. Could you explain it to me please?

Thank You for your help, I still don’t quite understand the runtime/edit stuff, however, with some shifting of my information into a script linked to the Pressure Plate, everything seems to be working!

Edit time means when you’re editing the scene/project. Sounds like that’s what you have done already!

Runtime would mean adding the script to the Pressure Plate after you start playing, through calling AddComponent<SomeScript>(); on it.

1 Like