try OnTriggerStay instead of OnTriggerEnter. I had the same problem, and the issue is that OnTriggerEnter only happens "Once". So once you are already inside the second trigger, it won't happen again until you first exit, then re-enter it. OnTriggerStay will fix that problem, as right after interacting with the first object it will detect that something is still within
#Triggers detection in Unity
1 messages · Page 1 of 1 (latest)
actually, it depends on what you do with the objects? if you don't remove them you might want to use OverlapShere (I'm sure there's a 2D version of it). But anyways that function returns an array of colliders, which you then can cycle through and activate all at once
The OnTriggerStay will work though, if it first checks if the object has been interacted with or not.
Thanks for your support, let me try it
This might give an issue where the two colliders constantly overwrite the value of the data type (IInteractive).
So what do you suggest ?
You can avoid that with bools I suppose. "isSelected".. or return if you already have a value
if(_interactive_Obj != null) return;//If I already have a value, don't search for more
Please explain how you want it to be. There are many ways to go about this but I wanna know which would be best for you.
This is also a plus one
well, of course i just want it to work properly, the _interactive_Obj could be switch right from the Collider to the next one without get null, is there any method that doesn't need to run every frames like OnTriggerStay()
To be fair, it doesn't run every frame ^^ it runs x-amount every second (every physics step). This is configurable in the settings
By default I think it's 50 times per second
I don't think there's a way that don't require to be called every frame, or every physics steps. Except: since you are using the Update() anyways, I suggest only calling the search and interact once when you press "x"
Pseudo code:
void Update(){
if(press x){
SeacrhForObjects();
if(found object)
Interact();
}
}
```This way, at least you won't do the whole searching every frame. This requires you to use the Overlap method <<---
well i think that i'll give it a try. Thanks guys. Really appreciate your assistance.
Just happy to help =) lmk if it works out
Here's another idea
Use the on-trigger enter and exit. When an object tries to exit the collider compare that object to see if it matches the object.
Does that work if you are within 2 colliders though? the way I interpret it is that : You interact with the first object, and if you try to press "x" again, nothing will happen because the _interacttive_object is either still the previous object or it hasn't changed to the new object
This would work with OnTriggerEnter2D alone and not OnTriggerStay
Yes, but would it work with OP's requirement?
i tried your way and it's working now, i guess i need more specific criteria when exit trigger to make it right. Again thanks for your help.