reset destroyed collider after game restart
Hello, I have a question. I make a game where the character scores by running through spawned 2d collider and thereby gets points. Method used "void OnTirggerEnter(Collider2D collider)" now the problem is that he can triggers one and the same collider twice and gets double points. I tried Destroy (collider); However, to destroy the once traversed collider, these are no longer available after a new start of the game. how do I restore the colliders after each game replay or respawn?
Answer by tormentoarmagedoom · Jun 26, 2018 at 02:57 PM
Good day.
You can just desactivate the objects containing the colliders, not destroy them.
Object.SetActive(false);
If all of them have the same tag, you can do this at the beggining of the new game
foreach (GameObject ObjectWithCollider in GameObject.FindObjectsWithTag("YourTag"))
{
ObjectWithCollider.SetActive(true);
}
Easy right?
The other solution is to desactivate only the collider component, by
Object.GetComponent().enabled = false;
And activate all them again at the beginig of a new game.
And the lastr solution, is to store all the Colliders that has been activated in a List, and always check before score if this collider is in the list
Bye!!!
Thanks buddy, your answer helped me a lot. It only took a while until everything worked out because I have respawning objects with collider and I have to activate the collider during the game. I have now solved that every time I spawned a new object, all colliders are set to active again. Only there is a problem that the collider is reset to the monent often when my character is in another collider.
i dont understand your problem.... collider reset? what you mean with reset? and if something is hapening when you dont want it, then you need more explicit code... make some "if" or things like this.
bye1