- Home /
Using a Single Collider to Detect Collisions with Multiple Colliders
I am currently working on making a script for picking up tools. My current design is attempting to use a collider in the parent object of the player to detect collisions with three different types of tools and change booleans accordingly.
function OnTriggerEnter (other : Collider){
if(other.gameObject == pickaxe){
canPickupPickaxe = true;
}
else if(other.gameObject == flareGun){
canPickupFlareGun = true;
}
else if(other.gameObject == radMeter){
canPickupRadMeter = true;
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject == pickaxe){
canPickupPickaxe = false;
}
else if(other.gameObject == flareGun){
canPickupFlareGun = false;
}
else if(other.gameObject == radMeter){
canPickupRadMeter = false;
}
}
However, when the player object collider collides with the tool's collider, the corresponding canPickup variable does not change. Is it possible to use a single collider to do this or have I made another mistake that is preventing this from working?
Answer by DanSuperGP · Jan 29, 2015 at 01:10 AM
if(other.gameObject == pickaxe){
This comparison operation will only return true if the variable pickaxe references the exact same instance as the gameObject you have collided with. If pickaxe is the prefab, and the gameObject you collided with is an instance of that prefab, it will return false.
If the pickaxe has the tag pickaxe you should be checking.
if(other.gameObject.tag == "pickaxe")
or, if you're doing it by name
if(other.gameObject.name == "pickaxe")
Notice in both of those examples I am comparing the strings for the name or tag with another string. Comparing strings with the == operator will do a value comparison and return true if the strings have the same value.
What your code is doing with the == is comparing two references, which will only refer true if they refer to the exact same instance. That's almost certainly not what you want to do.
That is true however I changed the code to using tags:
function OnTriggerEnter (other : Collider){
if(other.tag == "CollectPickaxe"){
canPickupPickaxe = true;
}
else if(other.tag == "CollectFlareGun"){
canPickupFlareGun = true;
}
else if(other.tag == "CollectRad$$anonymous$$eter"){
canPickupRad$$anonymous$$eter = true;
}
}
function OnTriggerExit (other : Collider){
if(other.tag == "CollectPickaxe"){
canPickupPickaxe = false;
}
else if(other.tag == "CollectFlareGun"){
canPickupFlareGun = false;
}
else if(other.tag == "CollectRad$$anonymous$$eter"){
canPickupRad$$anonymous$$eter = false;
}
}
And it still does not work.
The way I am referencing the object does not seem to be the issue as I have tried tags, names and references and none have solved the issue.
Ok, this should be perfectly obvious... but the collider this is attached to is a trigger correct... and has a rigidbody attached, and the pickups also have rigidbodies attached.
Also, it's a good idea to add a debug.log to the beginning of your OnTriggerEnter. Something like
Debug.Log("Trigger Entered : " + other.name);
That way you know that your OnTriggerEnter is actually happening, and it's not the comparison that is the problem.
Yes it is a trigger however I have never needed to use rigidbodies before for pickup objects. I made a key using a similar method which works perfectly and it does not have a rigidbody. I also did try the debug line and it yielded no useful information.
Answer by SnStarr · Jan 29, 2015 at 12:49 AM
try checking for the name of the game object for example:
instead of
if(other.gameObject == pickaxe)
{
canPickupPickaxe = true;
}
try:
if(other.name == pickaxe)
{
canPickupPickaxe = true;
}
because its already checking for the collider, just have it check for the name of the game object that the collider is attached to.
I just tested this out and it had no change, as the game objects are already assigned using tags earlier in the script.
don't you mean
(other.name == "pickaxe")
What you wrote
if(other.name == pickaxe)
Is comparing a string to a game object reference, which will of course always return false.
Answer by ThomasMarsh · Jan 29, 2015 at 01:56 AM
I managed to solve my own issue for this problem. I was using two different trigger colliders on the player object and the interaction between them was preventing my script from working properly. Thank you for the suggestions about referencing objects with triggers however.