Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Luke_Pit · May 14, 2020 at 03:44 PM · 2d gametriggercollision detectioncollider2dcollision2d

Setting a Prefab Clone as the Child of another Object on Collision (2D)

I am making a 2D Fishing Game, in which I am trying to have a Fish Prefab Clone become the Child Object of a Hook Object when they Collide. I have looked at a number of varying tutorials all of which don't seem to apply or work. This is the code I currently have, any ideas would be appreciated greatly.

 public GameObject GreyFish;

 void OnTriggerEnter2D(Collider2D collision)
 {
     if(collision.gameObject.tag == "GreyFish")
     {
         GreyFish.transform.parent = transform;
     }
 }
Comment
Add comment · Show 1
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 GenericToast · May 14, 2020 at 04:19 PM 0
Share

I assume this script is attached to the Hook Object? Inside your if statement do:

 collision.transform.parent = transform;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Reid_Taylor · May 14, 2020 at 04:33 PM

Well your code is kinda confusing but ill try and understand it.

First if all i'm guessing this is the fishing hook script in which it test for collision with fish. I can't really figure out the need for the GameObject GreyFish. Im pretty sure your code should look like this though

 public GameObject GreyFish;
  void OnTriggerEnter2D(Collider2D collision)
  {
      if(collision.gameObject.tag == "GreyFish")
      {
          GreyFish = collision.gameObject;
          GreyFish.transform.parent = transform;
      }
  }

I think its because public GameObject GreyFish isn't set yet.

But you could also just do it like this...

   void OnTriggerEnter2D(Collider2D collision)
   {
        // Compare tag method is more performant
       if (collision.gameObject.CompareTag("GreyFish")) 
       {
            collision.transform.parent = transform;
       }
   }

(Code not tested) Lemme know if i'm wrong...

Comment
Add comment · Show 4 · 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 Luke_Pit · May 14, 2020 at 05:15 PM 0
Share

Thanks this works for a new GameObject with the Tag, but It doesn't seem to work with a Clone generated from a Prefab with the Tag, the Hook GameObject just passes through.

avatar image Reid_Taylor Luke_Pit · May 14, 2020 at 05:18 PM 0
Share

$$anonymous$$ake sure the prefab has same tag and components.

avatar image Luke_Pit Reid_Taylor · May 14, 2020 at 05:35 PM 0
Share

The only difference is a Rigidbody 2D component which is being used in a $$anonymous$$ovement script for the Fish

Show more comments

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.

Follow this Question

Answers Answers and Comments

186 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

Related Questions

Collision stops working at certain situations. Help? 1 Answer

Intercept Collider 2D/Collision2D events with a single script on scene 0 Answers

Small 2D collider passing through other thin collider 2 Answers

How do I make object only collide with certain objects based on position of collision within the collider? 1 Answer

How to activate all GameObjects named ,,GroundCollider'' by touching ,,GroundTriggerCollider'' and deactivating when no longer touching ''GroundCollider"? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges