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 louisdeb · Feb 18, 2013 at 11:09 AM · c#collision

Problem with collision detection

Hey so I've got a player moving alone a straight line, and random cubes spawn in front of him randomly. As the player hits a cube it is destroyed and 'hit' is outputted into the console. Once this player has travelled a certain distance (~100), he is repositioned to the beginning of his path.

Again the cubes can spawn in front of him, however there is no collision detection and no cubes are destroyed..

The code for the collision detection (player is 'Player', and cubes are 'lasagnyi'). This script is placed as a spawn script on the lasagnyi gameobject, which refers to a lasagnyi prefab.

 GameObject player = GameObject.Find("Player");
         int r = Random.Range(0, 500);
         if (r < 10)
         {
             float x = Random.Range(player.transform.position.x, 40.0f);
             spawnPosition = new Vector3(x, 7, 0);
             GameObject myLasagnyi = (GameObject)Instantiate (lasagnyi, spawnPosition, Quaternion.identity);
         }

This code is for the destruction of the lasagnyi and is placed on the playerBehaviour script which is contained in the Player gameobject.

 void OnCollisionEnter(Collision collision)
     {
         Debug.Log("hit");
         if(collision.gameObject.name == "lasagnyi(Clone)")
         {
             Destroy(collision.gameObject);
         }
     }

The output 'hit' simply refers to a collision being detected, not the object being successfully destroyed (incase it came to mind that that was the problem).

Thanks for any help.

Comment
Add comment · Show 7
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 froYo · Feb 18, 2013 at 03:02 PM 0
Share

Hey, is the debug logging "hit"? or is it just not doing anything? If not I would make sure that there is a rigidbody attached the the object and that isTrigger is checked.

If it is logging collisions but not registering the if statement perhaps adding an extra bit to the

 Debug.Log("hit"+collision.gameObject.name) 

and checking to make sure you have the right name.

I would also suggest that maybe giving your prefab a tag and checking that would be better than checking the name

 collision.gameObject.tag == "Tag"

Sorry it's just a bunch of suggestions and not a solid answer :) hope it helps in some way.

avatar image louisdeb · Feb 18, 2013 at 04:10 PM 0
Share

So yeah I added the name to the output, and sure enough it is colliding with the lasagnyi(Clone), which is all fine.

I've had a look into tags but can't figure it out... to add a tag would I click on the lasagnyi prefab and there's a dropdown menu (with default Untagged)... but the options it offers are like 'Player' and '$$anonymous$$ain Camera'... which would I use, and if I were to add my own, how would I do that? It just takes me to a tags page..

Thanks for the help

avatar image froYo · Feb 18, 2013 at 04:32 PM 0
Share

To add a tag:

  • You click the drop down and click "Add Tag..."

  • Then at the top of the list you will see an arrow with the word Tags next to it

  • Click the arrow

  • You will then see "size" "Element 0"

  • Click on the "Element 0" field and type in what you want your tag to be

  • You can then go back to your prefab and select this new tag to apply it to you prefab

You can add in as many elements as you want here for future reference.

alt text

Also I would put a debug into the if statement just to check if it is actually working since the names match.

Example:

 void OnCollisionEnter(Collision collision)
 {
    Debug.Log("hit");
    if(collision.gameObject.name == "lasagnyi(Clone)")
    {
      Debug.Log("match");
      Destroy(collision.gameObject);
    }
 }

If the debug displays from there, then there is most probably an issue with the Destroy function.

avatar image CGPHANT0M · Feb 18, 2013 at 04:35 PM 0
Share

I don't see anything wrong with these scripts, they should work fine. It may have something to do with the player movement controller script. How are you re-positioning him?

Also, just go to Edit > Project Settings > Tags. Once that's open in the inspector, hit the drop down arrow next to Tags, then add your own Tags in the "Element 0: " entry, you can add more in element 1, 2, 3 etc.

avatar image Owen-Reynolds · Feb 18, 2013 at 06:08 PM 1
Share

If it works fine the first time, then the "redo" code is probably the issue. For all we know, just the model is brought back, leaving behind the collider.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

13 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

Related Questions

Problem Detecting 2D Collisions 1 Answer

Call a Void on Collision 2 Answers

Sounds triggering too fast 4 Answers

My weapon takes damage. help... 1 Answer

Missing GameObject and Null do not test the same, but how do I test for missing? 6 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