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.
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?
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.
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.