Need help with OnTriggerEnter

Hello community.

Please help me with OnTriggerEnter function.

In my project I want to do so that objects entering into trigger mesh Collider were sorted by tag and then, upon request of their components participated in the desired calculations.

The problem is that all the objects in the trigger located in one variable. (function OnTriggerEnter (other: Collider))

And I do not know how to (read) transfer link to the objects located in the trigger(that stored in “other”) to the array.

I think I understand now, but if this is not helpful, I’ll delete it. You want to track every object that enters the trigger for this particular object. The value passed in as other is of type Collider. Collider is extended from the Component class. See here: Unity - Scripting API: Component

Every object that of the type Component (almost everything you’ll work with), contains quick links, if you will, to anything on the game object. In other words, let’s say Object A is moving toward Object B, and Object A has a Box Collider, a Rigid Body, and a script called Aircraft on it. Let’s say Object B is your object that the script above is attached to, and it wants to know any time an airplane flies into it. If you set the triggers up properly (seems you don’t have any issue getting the trigger), then the Airplane’s Collider object is what is passed into the OnTrigger event as other. The “quick links” available to that are gameObject, rigidBody, transform, and collider. You can access any of these, and you’ll get that script’s reference from the airplane. (i.e. other.rigidBody, other.gameObject, other.collider (which will just give you other…:)). If you have other common Unity scripts attached to the game object (the airplane), you can access them as well (see the link script reference above).

Now you want to store a list of every airplane that hits Object B:

static List<GameObject> airplanes;

void OnTriggerEnter(Collider other) {
    if (other.tag == "Airplane")
        airplanes.Add(other.gameObject);
}

This will basically create a list of airplanes if the object flying into Object B has a tag of Airplane. What you do with that list is up to you. Also this is written in C#, so you’ll have to convert to JS. Also, it’s probably got errors, didn’t write this code in mono to test it, but the idea is there.

I have many objects in my scene with certain tag(Gear).
All these objects has same script that responsible for Gear to Gear behaviuor.
I want all these objects to be independent of anything.
So… this script must detect connections :

var Connected : float;
function OnTriggerEnter(other : Collider)
{
   if (other.tag=="Gear")
   {
      Connected = 1;
   }
   else
   {
      Connected=0;
   }
}

With this i have no problem at all but.
There also must be code that responsible for detect which objects with tag “Gear” entering the collider so i can take they link’s and continue with my calculation.(get these Objects scripts with their script’s varuables)
But now. If i have two object’s with tag “gear” i have them both in the (other.gameObject) so if i try to put one in array[0] and then with if statment second in array[1] if statement fail, because in (other.gameObject) stored both object’s.

var Connected : float;
var GA = Array();
GA.length=5;

function OnTriggerEnter(other : Collider)
{
   if (other.tag=="Gear")
   {
      Connected = 1;
      GA[0]=other.gameObject;
      if (other.gameObject!=GA[0])
      {
         GA[1]=other.gameObject;
   }
   else
   {
      Connected=0;
   }
}

In this exaple GA[1] never going to fill because (other.gameObject) always have these two object(for example) and they just put one or another many times per second.

I hope that was easy to understend now.(if no, comment)

I must also admit that if i try to (print(other.name)) there are 2 posts with name of obj1 and obj2

Sorry it’s taken so long for me to put my official answer in, here we go!

First Some code:

var Connected : float;
var GA : GameObject[];
var numGears : int;

function Start ()
{
   GA = new GameObject[5];
}

function OnTriggerEnter(other : Collider)
{
   if (other.tag=="Gear")
   {
      Connected = 1;
      var count : int = 0;
      for (count = 0; count < numGears; count++)
      {
         if (other.gameobject.name == GA[count].name)
            // We leave because it's already in the list.
            break;
      }
      if (count >= numGears)
      {
         GA[count] = other.gameObject;
         numGears++;
      }

   }
   else
   {
      Connected=0;
   }
}

So, the play by play, first, we have our variable declarations

var connected: float;

//the array of gear gameobjects to store.
var GA : GameObject;

//the number of gears currently in the array, we'll use this later to test to
//make sure we're not adding duplicate gears to the array.
var numGears : int;

//During the start function we want to set the array size. Notice when we created the array we’re declaring it an array of GameObjects, this is so that when we start testing the name variable we don’t have to create a temporary GameObject for the test each time it iterates through, saving valuable processing time.

function Start ()
{
   GA = new GameObject[5];
}

On to the meat of the code. When an object enters the trigger area we first test if it’s a gear. If it is then we begin iterating through the array to make sure it’s not a duplicate of an existing gear before adding it.

function OnTriggerEnter(other : Collider)
{
   // Here's where we check if the object that hit the trigger is a gear.
   if (other.tag=="Gear")
   {
      Connected = 1;
      //lets set count outside the for loop so that we can still use it's value
      //after the loop is finished.
      var count : int = 0;

      // Here's where numGears comes in handy, this way we don't end up iterating
      //through the whole array every time something hits. It will also help us
      //when we go to place new gears into the array.
      for (count; count < numGears; count++)
      {
         // Check if the names are the same, if they are we just leave the loop,
         //there's no need to continue. This is why we specifically declared
         //GA as an array of type GameObject, this way we don't have to create
         //a local variable to store the gameobject in before testing. As long
         // as all gears are GameObjects this should work 100% of the time.
         if (other.gameobject.name == GA[count].name)
            // We leave because it's already in the list.
            break;
      }
      // This is why we initialized count outside the for loop. When count is
      //greater than numGears it means we've checked every gear in the list 
      //already and the one stored in other is not in the array yet. We add it
      //to the array and increase numGears to reflect the new total.
      if (count >= numGears)
      {
         GA[count] = other.gameObject;
         numGears++;
      }

   }
   else
   {
      Connected=0;
   }
}

If removing gears from the array happens anywhere in your code be sure to decrement numGears as well as reordering your array. Say you have 3 gears in your array so numGears is 3 and GA[0], GA[1], and GA[2] all have gears assigned to them and you need to remove GA[1].

function RemoveGear (var gear : int)
{
   Destroy(GA[gear].gameObject);
   numGears--;
   while (gear < GA.Length -1)
   {
      GA[gear] = GA[gear +1];
      gear++;
   }
   GA[gear] = null;
}

This function may not work if copy pasted but should give you a good idea of how to go about removing gears.

The last thing is that you may want to use something like an arraylist to hold your gears. I don’t know the exact circumstances, but if you know you will only ever have 5 different types of gears then a static array will be fine. If there’s a possiblity that the number could change and you aren’t certain how many different kinds of gears you may have you could use it to dynamically add and remove the gears so you don’t get access violations.