Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 schadtaylor92 · Jun 11, 2014 at 07:05 PM · instantiatecollision detectionchild

Detect Collision with Instantiated Object Child

I have a prefab with a child that has a kinematic Rigidbody 2D and a Box Collider 2D that is a Trigger. The prefab gets instantiated using this script:

 static var canSpawn = true;
 var platform : GameObject;
 var heightMax : Transform;
 var heightMin : Transform;
 var time : float;
 
 function Update () {
     var mousePos = Input.mousePosition;
     mousePos.z = 10;
     var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
     
     if(Input.GetButtonDown("Fire1") && objectPos.y < heightMax.position.y - 0.5 && objectPos.y > heightMin.position.y + 0.5 && canSpawn){
         var clone = Instantiate(platform, objectPos, Quaternion.identity);
         Destroy(clone, time);
     }
 }

with platform being the prefab.

Attached to the player - which also has a kinematic Rigidbody 2D and a Box Collider 2D - is this script:

 var left : Transform;
 var up : float;
 var back : float;
 var time : float;
 
 
 function OnTriggerEnter2D (other : Collider2D) {
     if(other.transform == left){
         Debug.Log("test");
         ConstantMovement.canMove = false;
         rigidbody2D.velocity.y = up;
         rigidbody2D.velocity.x -= back;
     }
 }
 
 function OnTriggerExit2D (other : Collider2D) {
     if(other.transform == left){
         yield WaitForSeconds(time);
         ConstantMovement.canMove = true;
     }
 }

This works as expected when placing the prefab in the scene and attaching the child to the Transform left. However, when using it with the instantiated objects, it fails at "if(other.transform == left)". I believe this is because the instantiated objects are being created as clones. So how should I go about detecting the collision with the clones?

Comment
Add comment
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

1 Reply

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

Answer by Kiwasi · Jun 11, 2014 at 07:26 PM

Other.transfom == left will only return true if the transforms are the same GameObject

To check if they are made from the same prefab you can use tags or check the name.

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 schadtaylor92 · Jun 11, 2014 at 08:20 PM 0
Share

So I tried this.

 function OnCollisionEnter2D (collision : Collision2D) {
     Debug.Log("Test1");
     if(collision.gameObject == GameObject.FindGameObjectWithTag("Left")){
         Debug.Log("Test2");
     }
 }

I added the tag "Left" to the child of the prefab that's being instantiated. Well, needless to say it's not working. Without Googling and fooling around with it for another 4 hours, that's the best I've got. I'm quite new to all of this, so I'm sorry if I'm missing something obvious. Any advice would be appreciated.

Another, somewhat related question, wouldn't "collision.gameObject == GameObject.FindGameObjectWithTag("Left")" be an example of "hard coding" which should be avoided? Perhaps someone could clear that up for me. If it's not, what exactly is "hard coding"?

Edit: The Is Trigger has been removed from the child, of course. It passes up to Debug.Log("Test1").

avatar image schadtaylor92 · Jun 11, 2014 at 09:30 PM 0
Share

After taking a break and doing a bit of Googling, I found that the correct way (well, working in this situation anyway) to be collision.gameObject.tag. So the script is now this:

 function OnCollisionEnter2D (collision : Collision2D) {
     if(collision.gameObject.tag == "Left"){
         Constant$$anonymous$$ovement.can$$anonymous$$ove = false;
         rigidbody2D.velocity.y = up;
         rigidbody2D.velocity.x -= back;
     }
 }
 
 function OnCollisionExit2D (collision : Collision2D) {
     if(collision.gameObject.tag == "Left"){
     yield WaitForSeconds(time);
         Constant$$anonymous$$ovement.can$$anonymous$$ove = true;
     }
 }

However, isn't this still "hard coded"?

avatar image Kiwasi · Jun 12, 2014 at 09:01 PM 0
Share

Glad you got it figured. The second code is what I meant.

Rules against hard coding are slightly more flexible in game development. Efficiency is typically more important then reusability. DRY principles are still well worth following.

If you are concerned about hard coding then tag is a string, so you can fix it like this. (C#, but easy enough to convert using the docs)

 // This could be public as well if you wanted to assign it via the inspector
 private string left ="Left";
 
 void OnCollisionEnter2D (Collision2D collision){
     if(collision.gameObject.Tag == left){
         //Do all that other stuff
     }
 }



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

22 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

Related Questions

Getting instance of an sub object rather than the original's subobject 0 Answers

Renaming Child Objects during Instantiate? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Instantiate GameObject Parent targetting issue. 0 Answers

instantiated Gameobject trying to access child of child which is a clone itself. 2 Answers


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