Search Unity

[Probably a Unity bug] Object gets destroyed on collision even tho I'm not using Destroy() method.

Discussion in 'Scripting' started by marpione, Sep 21, 2017.

  1. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Hi, there I have decent knowlage of Unity and when my player collides with an object or when the game is over and I double click on the player in the hierarchy it gets destroyed. I have couple of scripts and I'm sure that I'm not using Destroy() method. I just can't figure out how this can happen. If any of you know why this is happening and have solution please let me know. I tried to put player to Don'tDestroyOnLoad but it's still happening. I also did find in files to check if there is a Destroy() method. I'm using Unity 2017.1
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Did you do a search for DestroyImmediate as well?
    Also, by "game is over" do you mean the Editor leaves Play Mode as well? Because if so, any objects created during Play Mode will not remain in the scene when you stop playing.

    Also, what version of 2017.1 are you on? 2017.1.1p2 is the latest patch release for 2017.1
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    When you double click the player in the hierarchy it gets destroyed? Do you have an editor script running that's doing something? That just sounds odd...
     
    Bunny83 likes this.
  4. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Invertex: I've just did a search for DestroyImmediate as well and nothing came up. Editor keeps staying in the game mode and Player is in the Hierarchy before entering the play mode. Gameover is a function I wrote and it basically toggles the player's sprite renderer controller ect. and I'm only pooling objects and that's where I create object in the during the game. I'm not creating nor destroying the player. During Run time it gets Destroyed. I'm using 2017.1.0f3

    Brathnann: I'm not using any editor scripts at all. I created this porject 2 days ago and I have 7 classes no plugings (exept sprites no code in there) and all of my classes are MonoBehaviour. I will try updating unity now maybe this will fix it.
     
  5. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Is there any way that unity can destroy something because of some reason on it's on did any of you experienced that?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Never heard of something similar either, are you sure there's really nothing that might destroy your object?

    Without any more details, it's difficult to guess and the usual answer will be "you're probably destroying it somewhere" as this would otherwise be a really weird bug that noone has heard of.
     
  7. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    That's what I'm thinking. Below you can see two scripts of objects that are being collided and also the gamemanger where I check if player lost the game or not. In the image you can see the Toggle Events that I'm doing in the game manager.

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3. void OnBecameInvisible()
    4.     {
    5.         GameManager.instance.GameOver();
    6.     }
    7.  
    8.     private void OnCollisionEnter2D(Collision2D collision)
    9.     {
    10.         GameManager.instance.GameOver();
    11.     }
    12. }
    Code (CSharp):
    1. public class OrbitController : MonoBehaviour {
    2.  
    3. #region PublicMembers
    4.     public float Gravity;
    5.     public float OuterAtmosphare;
    6.     public float InnerAtmosphare;
    7.     public bool isBlackHole;
    8.     #endregion
    9. #region PriviteMembers
    10.     bool pointIsGiven;
    11.     Transform PlayerStartPoint;
    12. #endregion
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         PlayerStartPoint = transform.Find("StartPoint");
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void FixedUpdate () {
    21.         ApplyGravity();
    22.     }
    23.  
    24.     void ApplyGravity()
    25.     {
    26.         Collider2D[] Objects = Physics2D.OverlapCircleAll(transform.position, OuterAtmosphare);
    27.  
    28.         foreach (Collider2D col in Objects)
    29.         {
    30.             if (col.tag == "Player" && GameManager.instance.isGameStarted)
    31.             {
    32.                 Camera2DFallow.instance.targetOne = this.gameObject;
    33.             }
    34.             Rigidbody2D colBody = col.GetComponent<Rigidbody2D>();
    35.             if (Vector2.Distance(transform.position, col.transform.position) > InnerAtmosphare)
    36.             {
    37.                 if (col.GetComponent<Rigidbody2D>() != null)
    38.                 {
    39.                     colBody.AddForce(Gravity * (transform.position - col.transform.position).normalized, ForceMode2D.Impulse);
    40.                 }
    41.             }
    42.             else
    43.             {
    44.                 if (col.GetComponent<Rigidbody2D>() != null)
    45.                 {
    46.                     colBody.AddForce(-Gravity * (transform.position - col.transform.position).normalized, ForceMode2D.Force);
    47.                 }
    48.             }
    49.         }
    50.     }
    51.  
    52.     void OnBecameInvisible()
    53.     {
    54.  
    55.         PoolingSystem.DestroyAPS(gameObject);
    56.  
    57.         pointIsGiven = false;
    58.     }
    59.  
    60.  
    61.     void OnDrawGizmos()
    62.     {
    63.         Gizmos.color = Color.red;
    64.         Gizmos.DrawWireSphere(transform.position, OuterAtmosphare);
    65.         Gizmos.color = Color.yellow;
    66.         Gizmos.DrawWireSphere(transform.position, InnerAtmosphare);
    67.     }
    68.  
    69.     private void OnTriggerEnter2D(Collider2D other)
    70.     {
    71.         if(other.tag == "Player")
    72.         {
    73.             if(Camera2DFallow.instance != null)
    74.                 Camera2DFallow.instance.targetOne = this.gameObject;
    75.  
    76.             other.GetComponent<PlayerController>().enabled = true;
    77.             GameManager.instance.SaveStartPosition = PlayerStartPoint;
    78.             if (!pointIsGiven)
    79.             {
    80.                 GameManager.instance.GivePoint();
    81.                 pointIsGiven = true;
    82.             }
    83.         }
    84.     }
    85.  
    86.     private void OnTriggerExit2D(Collider2D other)
    87.     {
    88.         if (other.tag == "Player")
    89.         {
    90.             other.GetComponent<PlayerController>().enabled = false;
    91.         }
    92.     }
    93.  
    94. }


    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2. #region Events
    3.     [SerializeField] ToggleEvent OnGameStart;
    4.     [SerializeField] ToggleEvent OnGameRunning;
    5.     [SerializeField] ToggleEvent OnGameOver;
    6.     #endregion
    7.  
    8. public void GameOver()
    9.     {
    10.         OnGameRunning.Invoke(false);
    11.         OnGameStart.Invoke(true);
    12.         OnGameOver.Invoke(true);
    13.         player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    14.     }
    15.  
    16. }
     

    Attached Files:

  8. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Also you might wonder what is DestroyAPS. It returns the objects to the Pool. Here is the code below. Just setting them inacitive.

    Code (CSharp):
    1. public sealed class PoolingSystem : MonoBehaviour {
    2.  
    3. public static void DestroyAPS(GameObject myObject)
    4.     {
    5.         myObject.SetActive(false);
    6.     }
    7. }
     
  9. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    One small note: I wrote a Instantiate function and call it OnDestroy and new object is not being destroyed on it's own. I guess that's the solution I found but still a wierd case
     
  10. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You shouldn't use the Engine's callback names if they're not doing what they're supposed to be doing.

    Once again, you haven't really provided enough information to continue looking for solutions. It's really difficult for everyone here to help you.
    You should try to track down the problem by removing objects or scripts partly from the scene. Other than that, at least Visual Studio also provides the option to search for words in the whole solution, have you tried that already?
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I agree with @Suddoha, if this is a bug, it's not one I've ever seen anybody on here mention. I'd be willing to look at the project if you'd like to upload it here, otherwise, I'm not sure what it is that would make your object be destroyed like that.

    I think he's using OnDestroy to recreate the object after it's been destroyed, which while I understand why he's doing that, it's certainly very hackish.
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Indeed a weird work-around for something that shouldn't occur in the first place. :D

    @marpione, have you already tried to attach the debugger to your project and track down the problem with it?
     
  14. elvismota00

    elvismota00

    Joined:
    Sep 21, 2017
    Posts:
    1
    Amigos, por que alguns scripts no monodevelop mostram a seguinte mensagem "sem conclusões encontradas" quando eu escrevo velocidade ou adicione e fica vermelho?
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're more-likely to get a quick answer to your question if you post it in English.
    Also, please create a new thread if this is either not related to this topic and/or a similar problem but a different context.
     
  16. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Hello everyone Thanks for the replys. I put an OnDestroy to debug the issue and than I used it to create a new object. It was an hack and I got an error massage that's saying "Some objects not cleared are you creating a new object on Destroy" I ignored it cause that's what I was doing.

    I found this threat and people were having a similar issue so I did the suggestions and remove and readd the trail renderer. That seems like fixed the problem. I was enabling and disabling the trail renderer and when I removed it nothing was destroyed. Than I added again and it's working now.

    Threat: https://forum.unity.com/threads/game-object-being-destroyed-why.313702/

    @Suddoha I shared some of my code and it's still waiting to be approved.


    Thank you so much for the answers and I hope this threate will help other people. To clearify. Remove the trail renderer and re add it. Try also removing sprite or mesh renderer and readd it as well. In some cases you might need to delete the prefab and create it again as well. I will mark this topic as solved. I also field a bug report and send the project to Unity. Hope they will find the solution.
     
    aBischuit and Ultimate_RedLad like this.
  17. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Where are you waiting for code to be approved? UnityAnswers?

    TrailRenderes have got a property autoDestruct that will - if enabled - ensure to destroy the GameObject they're attached to as soon as it does not move any longer (to be exact, once the trail is no longer present - given that it has already been present).
    For the pooling mechanism you may want to disable it (uncheck the box / set it to false).

    Just a convenience feature that exists since the TrailRenderer component itself knows best when the trail is no longer being rendered.
     
    Last edited: Sep 21, 2017
  18. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    I guess that was the problem. I may have checked it by mistake or something. I didn't know such property existed. Good to know and I will keep it in mind for the future. How do I close this thread? I can't select an asnwer as the best answer. That's my first time here.
     
    Suddoha likes this.
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    These threads never get closed or a "best answer". (like Unity Answers).
    They stay here for the future, and hopefully the "best answer(s)" is/are posted here and pointed out for future readers ;)
     
  20. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    I see Okay no proble so the solution is to check if Auto Destruct is true on trail renderer.
     
  21. laserbean00001

    laserbean00001

    Joined:
    Aug 23, 2020
    Posts:
    4
    A bit late, but for some reason, i had code on another game object that was completely commented out, but for some reason, it was still in effect.
     
  22. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I can guarantee that the 'effect' you encountered wasn't the commented out code running (assuming you have successfully compiled your project since commenting out the code and saving the changes.)