- Home /
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.
You question is confusing. As a fuzzy guess to what you are looking for, you can use the collider to get a component.
var someComponent = other.GetComponent(SomeComponent);
This allows you to communicate directly with a component on an object that entered the trigger zone.
Robertbu, I know how to get other GameObject component. I do not know which object is in contact with the trigger, so I can not know which object I have to ask for a component.
The problem is that in trigger zone can be located more than one object. And I need all the objects with a certain tag, which are in the trigger.
use other.tag or other.gameObject.tag to get the tag of the other object. Is this what you mean?
Do you want a copy of the object in the new array or a link to the actual object?
TykoX64, a link to an object!S! yes. If there was one object that would not be a problem. But there many, and i can't sort them properly because i don't know how to read content of trigger.
Answer by TykoX64 · Mar 26, 2014 at 04:16 AM
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.
I alrady told you that you can't access to object component in the array like that. And you repeat that wrong syntax twise.
Destroy(GA[gear].gameObject);
if (other.gameobject.name == GA[count].name)
Also you have another mistake in declaring the loop.AGEAN.
for (count; count < numGears; count++)
Thanks, I copy pasted most of the code from one of my comments and forgot to add =0 to the for loop.
As for the array of GameObjects, you can access properties with GA[count].name, that's why I declare at the beginning that it's an array of type gameobject. Notice the difference:
//Explicit declaration
var GA : GameObject[];
//Inferred Declaration
var GA = new Array();
Answer by supernat · Mar 20, 2014 at 06:53 PM
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: http://docs.unity3d.com/Documentation/ScriptReference/Component.html
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.
Supernat, thx for reply.Well, i see you don't understand my point at all...
Wait, I'll try to explain more clearly, with examples.
Answer by GUNrcTeR · Mar 20, 2014 at 08:08 PM
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
So to try and understand, what you're saying is that you are passing multiple objects with the same tag through the trigger. You want to store them in separate "slots" in the array but are unsure of how to do so as the objects are all identical?
TykoX64, correct. They have diffirent name but tag the same. Yes i want take these objects from trigger and use them further in script to call these object component.
But i dont know how to take them from trigger because trigger cannot be read like an array or ToString.
after you deter$$anonymous$$e that it's a gear you can check it's name and if it's not equal to any other name in the array then add it to the end. It also sounds like a List may be better than a static array.
var Connected : float;
var GA : GameObject = Array();
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; 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;
}
}
Check out Array Lists here:
TykoX64 it's late let's continue tomorrow. Big thx for participating.
Btw - Unity write "BCE0034: Expressions in statements must only be executed for their side-effects."
on " for (count; count < numGears; count++)"
Absolutely. I'm happy to help and want to make this work for you.
as far as the error I mistyped the for loop. try setting count to 0 initially.
for (count = 0; count < numGears; count++)
the reason for the error was that the first part of the loop
for (count;
doesn't actually do anything with count so it registered as an error.
For the rest of the code, what it does is any time a collider hits the trigger it checks to see if it has the "Gear" tag. Once it verifies that it is a gear it sets connected to true, creates count and sets it to 0, then enters a loop to check the other collider against anything that is already part of your array.
If the other.collider's name is the same as GA[count].name then it's the same gear and you already have a reference to it in your array so we quit searching. If we don't find one that matches we exit the loop and add it to the next "slot" in the array.
This method will not EVER overwrite any GameObject that is already in the array. Why I suggest a list is that you can use List.Add() to put the gears in the arrayList and use List.Remove() to remove them without having to keep track of which "slots" are actually open.
Once you have the GameObjects put in the array you can access them from anywhere else in the code by calling the gear's associated "slot" in the array.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140735im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Basic javascript array declaration 2 Answers
Shotgun using array/list in Javascript 1 Answer
Convert Array into One String (Js) 1 Answer
UCE0001 and BCE0044 Errors 1 Answer
Reading and Storing External Data into Memory (From Text) 1 Answer