• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OfficerNumberOne · May 20, 2019 at 02:03 AM · collisionprefabarraygetcomponent

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadDevil · May 20, 2019 at 03:32 AM 0
Share

On top of my head i can think of two ways.

1) Check by the name of the tree if all the tree has different naming convention collider.name == "tree name" 2) if you are checking for only specific tree then you can assign a tag to the object and check for that tag. collider.tag == tree.tag

avatar image Bunny83 · May 21, 2019 at 05:59 PM 0
Share

Your use of terminology is confusing. A player can never collide with a prefab since a prefab is a template for an instance. You may referring to instances of a prefab? At runtime an object doesn't have any knowledge from which prefab it was cloned from.


So i guess your "treePrefab" array actually contains just prefab instances which are placed in the scene? Whats the type of your "treePrefab" array? You directly compare the reference with the collider you collided with. So if your treePrefab array is anything else than a Collider[] this of course will not work.


There simply isn't enough information in your question. Also please readd your code and mark it as code. Just select all your code and press the 101 / 010 button. Otherwise things in angle brackets <> will get stripped away since it will be treated as html.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Meishin · May 21, 2019 at 08:39 AM

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();
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OfficerNumberOne · May 21, 2019 at 01:44 PM 0
Share

$$anonymous$$aybe this will help explain @$$anonymous$$eishin :

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.

alt text

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

alt text

Hope this helps explain my issue. Thanks!

screenshot-22.png (34.2 kB)
screenshot-12.png (58.1 kB)
avatar image Meishin OfficerNumberOne · May 21, 2019 at 05:18 PM 0
Share

Yeah i had understood correctly ; Indeed no loop is required since the collider is the one of the specific Tree you want to degrade :)

avatar image OfficerNumberOne Meishin · May 22, 2019 at 01:27 AM 0
Share

@$$anonymous$$eishin,

Thanks man, finally got it to work properly and completely did away with the Array, as it was redundant and confusing. Wouldn't have figured it out without your help.

avatar image
1

Answer by RustyCrow · May 21, 2019 at 02:28 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

collision combo 1 Answer

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Can I use the mass-tree place tool to place something like a rock? 3 Answers

Collision with my first object of array or second... 1 Answer

How to reduce usage of GetComponent()? (instantiate, prefabs) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges