How to determine if a Player collides with a particular Prefab in an array of Prefabs?

I am creating an adventure/crafting game and I need a section of code that determine if the player is colliding with a specific Prefab in an array of Prefabs. I know that the for loop is working, it correctly cycles through based on the number of trees drawn in the game. However, the if statement does not identify which treePrefab in the array the Player is colliding with. Please help!

treeDrawn = number of trees Instantiated
treePrefab = an array of treePrefab of size 11
treeLogic = a seperate script that owns function DegradeTree()
DegradeTree() = a function that weakens the target tree when called

Code:

for (int treeFinder = 0; treeFinder <= treeDrawn; treeFinder++)
{
if (collisionInfo.collider.Equals(treePrefab[treeFinder]))
{
treePrefab[treeFinder].GetComponent().DegradeTree();
}//end if
}//for

Hello @OfficerNumberOne,

Best guess treePrefab[treeFinder] is a GameObject and you’re comparing it to a collider. collisionInfo.collider.GameObject instead ?

That said, If i understand correctly DegradeTree is a method on the Tree itself ? Since this Tree itself has a collider (i guess), why bothering with the tree prefab ? You can check directly if player has entered collision with the tree, for example by checking object has a tag or class component containing the DegradeTree :

if (collision.transform.tag == "Tree"){
   var treeclass = collision.transform.GetComponent(DegradeTreeClass);
   if(treeclass  != null){
        treeclass.DegradeTree();
    }
}

Unless i am misunderstanding you @OfficerNumberOne from what i am reading you might be misunderstanding collision ? every tree can handel its own collision so you dont need to loop trough every tree to find which the player hit. everything inside the OnCollisionStay will run ONLY for the tree hit. So you can just call the DegradeTree() from inside the OnCollisionStay and it will only happen on that tree. Check out som tutorials for a better examples you get it in no time .

Now if i am misunderstanding, and you are trying to make 1 specific tree a “special” i would make the tree unique somehow. The comment by @MadDevil is one way to compare (through names) but its scary because names change and you might forget. Tags serve a similar purpose but same issue although safer. I would suggest something like saving the tree you want and then using your loop to compare all the tree objects → check this out. Then break out of the loop when i find and degrade it.

Maybe this will help explain @Meishin :

This is where the treePrefabs are instantiated and added to an array. Im using an array to track and target which particular tree the player is colliding with. Otherwise, without an array, every single tree would take damage.

And this is where I attempt to identify which treePrefab the player is colliding with and then remove health from that tree.

Hope this helps explain my issue. Thanks!