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 xxluky · Feb 20, 2015 at 02:22 PM · c#collisiondestroy

My player is suddenly and rarely destroyd when collecting coins

Hi everyone, Is there a way how to find out what or who destroyed my player? I suggest to use function OnDestroy() and print but what? I prefer C#.

My problem is that I got a 2D scroling car game (using 3D platform). Actualy player stays and moves only on the X axis and everything else is moving towards the player using Random for creating new opponent cars and coins. The player collects coins for more points. Player has BoxCollider - trigger and coins are prefabs with BoxCollider - trigger too. Everything works fine but sometimes when the player collect the coin, player and the coin are suddenly destroyed! I have no idea why. It is strange - game is normaly working but sometimes the player disappears, not always only rarely.

Here is the key code but when the player is suddenly destroyed (removed from the hierarchy) there is no sound and no explosion:

     void OnTriggerEnter(Collider other)
     {
         if(other.tag == "Enemy")
         {
             Instantiate (explosion, rigidbody.position, Quaternion.identity);
             gameController.GameOver ();
             Destroy (other.gameObject);
             Destroy (gameObject);
         }
         if (other.tag == "Coin")
         {
             gameController.AddScore(scoreValue);
             Destroy(other.gameObject);
             audio.Play();
         }
     }
Comment
Add comment · Show 13
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 Mmmpies · Feb 20, 2015 at 02:27 PM 0
Share

First thing to do is Debug.Log it, chances are one or more of your coins is tagged Enemy.

avatar image xxluky · Feb 20, 2015 at 02:39 PM 0
Share

Thank you for your reply. There is only one coin as prefab tagged coin. The script is always instantiating this coin usin Random for random time and position. If any of the coins was tagged "enemy" only by mistake, there would be sound and explosion but non of them are shown. I thing this is some kind of Unity bug...

avatar image xxluky · Feb 20, 2015 at 03:19 PM 0
Share

I really dont understand to Debug.Log("Script Name : Function Name : Tag Name") - where should I put it?

I put this into the OnTriggerEnter function: Debug.Log(other.gameObject). Everything the player touches it is written in the console. But when the plyer suddenly dissapears it says nothing. The player dissapears just when it hits a coin but at this point it even does not print that the player touched this coin that made him dissapear...

I searched for destoy - there is only one more. The whole game is in boundary that destroys evering that comes out of it:

 public class DestroyByBoundary : $$anonymous$$onoBehaviour {
     
     void OnTriggerExit (Collider other)
     {
         Destroy (other.gameObject);
     }
 }
avatar image Nymisu · Feb 20, 2015 at 03:45 PM 0
Share

Not a Unity bug, as said prior. The only chance of this happening is that there is a coin that's tagged as an enemy. Note that the explosion is instantiated at the position of the rigidbody, ins$$anonymous$$d of the transform. If so happens the coin doesn't have a rigibody, Unity throws a nullreferenceException, and possibly also animates the explosion at 0,0,0 coordinates in worldspace.

Add your debug.log within the enemy if statement.

avatar image Mmmpies · Feb 20, 2015 at 04:06 PM 1
Share

That code is also part of the space shooter tutorial, it's used to stop bullets from hanging around once they get to the edge of the screen, it should stick with the camera but that doesn't mean yours does. I agree with @hexagonius it's the most likely candidate for the cause rather than a Unity bug. Are you sure you don't need it for something else though?

EDIT

Put the

Debug.Log(other.name + " was DestroyedByBoundary at " + Time.time);

in the OnTriggerExit, and if it was that that line will appear in the console. If the Time.time matches how long the game was running before the player vanished and more importantly if the Player name appears then that's what caused it.

Show more comments

1 Reply

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

Answer by Mmmpies · Feb 20, 2015 at 02:51 PM

OK then that's unlikely but do Debug.Log("Script Name : Function Name : Tag Name"), well I don't know your script name but "MyScriptName : OnTriggerEnter : Enemy" and another one for Coin.

Assuming you're using Mono click Search -> Find in Files and search for Destroy and debug any others that you find and see if it debugs what kills your player.

Also run the game without Miximize so you can flick to the Editor and see if your player has vanished whilst the game's running.

EDIT

Anywhere you have a Destroy command put a Debug.Log just above it that shows the script/function and what test has been done (Enemy/Coin). All it does is output to the console like an error message, the last one that appears when your player disappears should be what's killing the player (if the player is being killed). It's just diagnostics to see if we can narrow down what's happening.

EDIT

OK I converted this to the answer as it best shows to Debug that was needed to find the issue but I'll add the actual Debug line that proved it:

 Debug.Log(other.name + " was DestroyedByBoundary at " + Time.time);


Glad you got it sorted.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Simple on Collision Help (C#) 2 Answers

Removing an object if it spawns in a building 1 Answer

gameObject doesn't self-destruct after collision c# 2 Answers

Multiple Cars not working 1 Answer

Instantiate 1 object after 2 objects collide. ( C# ) 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