Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

adding objects inside a trigger to an array

Discussion in 'Scripting' started by ratamorph, Aug 22, 2008.

  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I'm in need of a way add objects that are inside of a trigger to a collection without repeating them.

    In the docs it says "OnTriggerStay is called once per frame for every Collider other that is touching the trigger."

    So I could add the object inside the trigger to the collection this way and since it's executed once for every object it would add all objects inside the trigger to the collection. The problem is that I want to clear the array before I start adding duplicates to the collection but I don't know where to do this.

    Ideally I would like to use on OnTriggerStay to add all objects to the collection, perform a function on the whole array, like moving them and such and then clear the array and then refill it with the object inside the trigger for the next time I perform such function.

    I hope I was able to explain it well, Anyone know a way to do this?
     
  2. gizzerd91

    gizzerd91

    Joined:
    May 4, 2008
    Posts:
    311
    Aren't collision reported in arrays if you get them from the object with the trigger?
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    So wait, you want a list of objects that are inside a trigger? Try:

    OnTriggerEnter - Add to array
    OnTriggerExit - Remove from array
     
  4. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    is it? how do you do it?
     
  5. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I gave the OnTriggerEnter and OnTriggerExit aproach some thought it could work this way, but I'll be testing against all objects inside the trigger this script would be attached to, so wouldn't I have to loop trough all objects in the array to know which object to remove? I guess I'm just confused here because the aproach makes soo much sence.
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, you add them all to the array as they enter, checking each time with a for loop to add them to the first open spot. It's the same with the exit. You find the one that == it, and remove it.

    This only gets processor intensive if you have hundreds of these passing in and out constantly.
     
  7. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    576
    I'm not much of a programmer - still learning - but this works for me and I think it does what you are asking:

    Code (csharp):
    1. #pragma strict
    2.  
    3. // connect to appropriate components via inspector
    4. var hockeyPlayer2 : HockeyPlayer2;
    5. var sphereCollider : SphereCollider;
    6.  
    7. // expose these variables
    8. var speed : float;
    9. var entityCount : int;
    10. var entities : Transform[];
    11.  
    12. var regionFactor : float;
    13.  
    14. function Awake()
    15. {
    16.     sphereCollider.isTrigger = true; // set this in the inspector, redundant check here
    17.     regionFactor = Random.Range(.25, 1.25);
    18.     entityCount = 0;
    19. }
    20.  
    21. function FixedUpdate()
    22. {
    23.     speed = transform.parent.rigidbody.velocity.magnitude;
    24.     sphereCollider.radius = speed*regionFactor;
    25. }
    26.  
    27. function OnTriggerEnter (entity : Collider)
    28. {
    29.     if (entity.tag == "Body") // just check for the body collider in player
    30.     {
    31.         entityCount++; // increment count since we have a PlayerGO entering the collider
    32.  
    33.         if (entityCount == 1) // no previous neighbors
    34.             entities = new Transform[entityCount]; // resize the array
    35.         else
    36.         {
    37.             var oldEntities : Transform[] = entities; // save the previous neighbors array in oldNeighbors
    38.             entities = new Transform[entityCount]; // resize the neighbors array
    39.  
    40.             var i : int = 0; // initialize the index
    41.             for (var oldEntity : Transform in oldEntities) // iterate through the oldNeighbors array
    42.             {
    43.                 entities[i] = oldEntity; // assign the oldNeighbors to neighbors
    44.                 i ++; // increment the index
    45.             }
    46.         }
    47.        
    48.         var newEntity : Transform = entity.transform; // get the new hockeyPlayer who has entered the collider
    49.         entities[entityCount-1] = newEntity; // add it to the array
    50.     }
    51. }
    52.  
    53. function OnTriggerExit (entity : Collider)
    54. {
    55.     if (entity.tag == "Body") // just check for the body collider in player
    56.     {
    57.         entityCount--; // decrement count since we have a PlayerGO exiting the collider
    58.  
    59.         if (entityCount) // if there are neighbors
    60.         {
    61.             var oldEntities : Transform[] = entities; // save the previous neighbors array in oldNeighbors
    62.             entities = new Transform[entityCount]; // resize the neighbors array
    63.             var i : int = 0; // initialize the index
    64.             var exEntity : Transform = entity.transform; // get hockeyPlayer who has exited the collider
    65.             for (var oldEntity : Transform in oldEntities) // iterate through the oldNeighbors array
    66.             {
    67.                 if (exEntity != oldEntity) // is this oldNeighbor the exNeighbor?
    68.                 {
    69.                     entities[i] = oldEntity; // if not, assign this old neighbor to neighbors[index]
    70.                     i++; // increment index
    71.                 }  
    72.             } // exNeighbor has been removed from neighbors array!
    73.         }
    74.         else
    75.             entities = new Transform[0];
    76.     }
    77. }
     
  8. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Thanks to all the good advice from you all it works now, I was kinda hoping for a way to do it without the for loop but this works and it's true that it only gets intensive if I have a lot of intersecting objects which shouldn't be too bad in my case.

    Thanks all