Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Getting trigger enter from outside the GameObject

Discussion in 'Scripting' started by Kinkku2, Oct 26, 2015.

  1. Kinkku2

    Kinkku2

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey! I'm trying to refer to multiple GameObjects in script and would like to refer to their 'trigger enter' functions from a script that is not inherently linked to the specific GameObject itself. I have a list of the needed GameObjects in the script and would like to check if anything enters their triggers.

    Would I have to do this with bounds or is there another way to execute this? If I do it with bounds, how would I go about checking if absolutely any GameObject enters without a list of all the GameObjects? Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this sounds awfully like trying to do things backwards... why are you trying to do this?
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,586
    You could make a proxy script that gets placed on all of the specified triggers and fires back to the parent script when something happens.

    However, this does indeed seem a little backwards. Why do you need to to work this way?
     
  4. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
  5. Kinkku2

    Kinkku2

    Joined:
    Sep 29, 2014
    Posts:
    106
    Yeah that's true, it does feel a bit backwards to be honest. :confused: To give a bit more in depth view into my problem: I'm trying to get everything to wrap around the edges nicely, coming through from the right side if you went out of bounds off the left side. I was thinking of basically having one script for wrapping every object around the edges. I was at first creating a list for all the GameObjects currently active in the scene and just wrapping them from that list, but then I came to the conclusion that it would probably be a lighter load for the game if the check only fired when a certain position has been entered. Not sure if I'm even right on that, though!

    I guess could do this with a Vector2 clamp instead of four triggers in the scene as well, but not sure how to get the clamp to fire only when its min or max value has been reached without keeping an active list of the GameObjects' positions. So, I made the four edge triggers that fire off the screen wraps (transform.position to the opposite of the current) when entered and would like to keep track if they have been entered from that one script rather than having a script attached to them all. There could be a totally different solution to this problem as well!

    Events could be good as well, but I think that would require a subscriber script to be attached to each one of the triggers. And that's not too much at all for four colliders, just wondering if I could keep it to one script.
     
    Last edited: Oct 26, 2015
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can have the script on the four boundary triggers, you just use the "other" gameobject (if you're using the same naming convention as in the scripting reference)

    http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    i.e.
    "if something collides with me, move that thing x units that way" rather than
    "if I collide with something do some weird complicated voodoo stuff"
     
    CrymX likes this.
unityunity