• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
       
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Delete causing crash because of event, delayed delete needed ?

This topic is 403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
Having an event OnCollision I can check if one collision happens but if I do "delete this;" in this event a crash happens because the update of the active transforms from physx is invalid.
Basically the physx update is :

updateScene
fetchResults
update actors with active transforms from the scene update
update actors with controllers

The only way I see to solve this issue is to add a Destroy function which delete the actor at the end of the update, like Unity does with "Destroy(this);".
Maybe same kinds of issue can happens with other situation so maybe the delayed destroy is a good way.
Another way or ideas, or this way is a good way ?
Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement
In addition to using shared_ptr, replace "delete this;" with "Tell my owner(s) to remove me."

This kind of thing is sometimes implemented as a "return false;" inside an Update method. Whoever calls Update is either the owner or can propagate that return value upwards to the actual owner. Edited by Nypyren

Share this post


Link to post
Share on other sites

One Actor contains one physx component like SphereCollider, the physx actor is linked to the actor using the user data pointer.
The "delete this" is used in the OnCollision event of the actor when the object collide with something.
The event of the actor is called by the event callback of physx, surely on the fetchResults.
After the update + fetch of physx scene, actors are updated using the user data.
Destructor of the actor remove from the scene, so "delete this" remove from scene + delete also all components.
But since the physx actor and shape is also deleted because if you delete the actor you delete all the components too, physx crash.
One solution to avoid the "delete this" is to change one boolean to true instead of "delete this", like NeedDelete to true and delete later.
This solution is actually possible on my actual design because I use a game state with an update, but if all is managed by the scene you have to call "delete this".
It's already the case when you do a game with unreal or unity, you call destroy( this ) on the actor event / gameObject event on the same situation.
I'm not sure game-state is a correct way to continue on a modern design because you can do all with simply the scene.

Edited by Alundra

Share this post


Link to post
Share on other sites

It's already the case when you do a game with unreal or unity, you call destroy( this ) on the actor event / gameObject event on the same situation.


That's because 'Destroy(this);' does something *completely* different than 'delete this;'. :)

'delete this' is inflexible because the destructor and memory free is going to happen NOW. You are correct that you need to wait to do that until everything else is finished using the instance *or anything the destructor cleans up*.

For example, here's Unity's chart of what happens in which order:

https://docs.unity3d.com/Manual/ExecutionOrder.html


Let's say you call Destroy(this) in any of those physics-related functions. Unity marks the object to be destroyed, but does not destroy it until it gets to the very end of the frame (it calls OnDestroy, THEN actually deletes the object). The object that "got destroyed" will still be usable until that point. Edited by Nypyren

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement