Nested OnTriggerStay not working

I’ve created a script which contains all the functions i want to be called by the unity event manager. In this “actions”- script I’ve got one function which should open a textbox after checking if the gameobject infront of a different object is the player. Im using a nested OnTriggerStay for this. I got one warning in the editor: “Assets\Scripts\Event Manager Stuff\Actions.cs(14,11): warning CS8321: The local function ‘OnTriggerStay’ is declared but never used”

using UnityEngine;
using DialogueEditor;
public class Actions : MonoBehaviour
{

   


   public void OpenTextbox(NPCConversation myConversation)
   {
    
    Debug.Log("Searching for player");

     void OnTriggerStay(Collider other)
      { 

         if(other.CompareTag("Player"))
         {
            ConversationManager.Instance.StartConversation(myConversation);
            Debug.Log("Dialogue started");
         }
      }

   }
}

Even if the Player is inside the trigger, nothing happens except the searching for player message.
I don’t know if its even possible to nest OnTriggerStay. If not, do you know a option to check what object is in the trigger at the moment?

Your OnTriggerStay() is inside another function.

Put it outside like normal functions in the class.

Only normal functions can be seen by Unity’s inspection.

You cannot nest it. To implement what you want you could raycast to see who is infront of the NPC, or you could store in a field for the NPC the players that enter and exit the collider with OnTriggerEnter and OnTriggerExit

1 Like

That’s what I thought. Since I’m using a 2d sprite as the player, raycasting could become quite difficult. I’d need to do it unless there is a simpler solution.

What do you mean by storing in a field? And wouldn’t I have the same problem if I used an on trigger enter or exit?

I want the trigger only to be checked when OpenTextbox is called, not every frame.

This is just basic “press E to interact” functionality. Go check any basic tutorial for this stuff. The answer will not only be in code. Traditionally one uses OnTriggerEnter if you only want the leading edge, or you have different scripts responding that can be disabled or inhibited when you don’t want to hear from them.

I know that this is a basic press E to interact functionality. I’d do it differently if I only planned on using it for this. However, this is an extension to my Event system. I want the onTriggerStay function in OpenTextbox to be called just once. Since that’s not possible I need to find an option.

The Unity physics system looks in specific places for methods to call.

Inside of other functions is not one of those places.

I guess you’re just not reading what I posted above…?

OnTriggerEnter() does exactly this, as I point out here:

If that does not suit your needs, it is on you to explain why, otherwise we’re just going to keep telling you how it is normally done by normal programmers solving normal problems in a normal Unity setup.

Ok I’m sorry if I explained it badly, I believe I didn’t add enough context: I‘ve got an event system based on scriptable objects.

I apply a listener script to specific game objects. As soon as a Game event is called, for example „interact“ by the player, every game object with a listener listening for „interact“ runs a function out of my actions script. This actions script contains the OpenTextbox function.

In front of my NPC is a trigger.
Whenever the player presses the interact button, the interact game event is broadcasted to the npc, who then should check for game objects in its trigger.

Since I can’t use on triggerStay like I thought I could, I need an option. Writing it outside the OpenTextbox, would be an option, but would mean I need to make changes in my event system, which might be tedious to do.

So, unless there is no simple workaround, more difficult option would be to do it via raycasting or by changing my event system.