Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Unsure how to receive OnTriggerEnter from a different object.

Discussion in 'Scripting' started by Callum_Coombs, Apr 14, 2021.

  1. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    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:
    Code (CSharp):
    1. IEnumerator IntroTutorialTextsAndEnemies2()
    2.     {
    3.         Debug.Log("CoroutinePart2Start!");
    4.         PressurePlate.gameObject.GetComponent(OnTriggerEnter);
    5.     }
    What I know I need to do on the Other Script:
    Code (CSharp):
    1. public void OnTriggerEnter(Collider other)
    2.     {
    3.         PressurePlateOn = true;
    4.     }

    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.

    Code (CSharp):
    1. Debug.Log("CoroutineStarted!");
    2.         //PRESSUREPLATE_ONTRIGGERENTER=TRUE//
    3.         Intro1.SetActive(true);
    4.         yield return new WaitForSeconds(10);
    5.         Intro1.SetActive(false);
    6.         Intro2.SetActive(true);
    7.         yield return new WaitForSeconds(10);
    8.         Intro2.SetActive(false);
    9.         PressurePlate.SetActive(true);
    10.         Debug.Log("CoroutinePart1Over!");
    Please Help.. I need it soon as well! Have a good day!
     
  2. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    I might have found something.
    Code (CSharp):
    1.  void OnTriggerEnter2D(Collider2D collider){
    2.    
    3.        if (collider.name == "Wall") {
    4.          //Do something with Wall or your character
    5.                
    6.        } else {
    7.    
    8.              if (collider.name == "Floor") {
    9.             //Do something with floor or your character              
    10.                    
    11.        }
    This was in a script from unity answers (https://answers.unity.com/questions/681599/index.html)

    Still unsure how to implement anything...
     
  3. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    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.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    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?
     
  5. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    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?
     
  6. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    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!
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    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.
     
    Callum_Coombs likes this.