- Home /
Problem is not reproducible or outdated
How to call on collision enter only once
i have player which when collides with an enemy, the onCollisionEnter function is called. I want that when it is called a heart should be destroyed. also both the players are cubes. This is the code for the onCollisionEnter method:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name.Equals("player"))
{
heartscript.livesLost += 1;
Debug.Log(heartscript.livesLost);
}
}
This is the code for heart script
public static int livesLost = 0;
public GameObject heartOne, heartTwo, heartThree;
void Update () {
transform.Rotate(new Vector3(0,0,90)*Time.deltaTime);
if (livesLost == 1)
{
Destroy(heartOne);
}
else if (livesLost == 2)
{
Destroy(heartTwo);
}
else if (livesLost == 3)
{
Destroy(heartThree);
}
}
Problem is that every collision the value of lives lost becomes 20 so in one collision only i lose all 3 LIVES Any help would really be appreciated. Thanks !! Please!!
Doesn't look like an issue in the script, are you sure nothing else is modifying the livesLost?
@manaslegodesigns I cannot reproduce the problem. It's not the scripts.
Answer by Saucyminator · Dec 05, 2017 at 04:50 PM
Try this (FYI: this uses the tag system, so tag the player with a "player" tag):
public class EnterOnceCollision : MonoBehaviour {
private bool hasEntered;
void OnCollisionEnter (Collision other) {
if (other.gameObject.CompareTag("player") && !hasEntered) {
hasEntered = true;
heartscript.livesLost += 1;
Debug.Log(heartscript.livesLost);
}
}
}
Awesome dude, thanks i got it to work. I also rewarded u with some points. Thanks a lot
In the repro project I created to reproduce your problem, everything worked properly with your code. No need for the tag and the hasEntered flag. OnCollisionEnter was always called once as it should.
I guess the tag is needed to be sure it's only the player that can trigger the collision, otherwise anything that collides with it would trigger the livesLost += 1;
But yeah, if OP decides to use Destroy() to remove the gameObject after it has been triggered and livesLost += 1 been called, hasEntered wouldn't be needed.
Debug.Log(other.gameObject.name);
The results will sometimes surprise you.
Answer by danteswap · Dec 05, 2017 at 05:18 PM
Ok you need To Use Both OnCollisionEnter And OnCollisionExit For it To Work So Use like this :
bool Hitted;
private void OnCollisionEnter(Collision collision)
{
if ((collision.gameObject.name.Equals("player")) && (Hitted == false))
{
Hitted = true;
heartscript.livesLost += 1;
Debug.Log(heartscript.livesLost);
}
}
private void OnCollisionExit(Collision collision)
{
if ((collision.gameObject.name.Equals("player")) && (Hitted ==true))
{
Hitted = false;
}
}
Answer by Full8uster · Dec 05, 2017 at 04:28 PM
Try create a bool var on your script where OnCollision is. Then you try it:
private void OnCollisionEnter(Collision collision)
{
if ((collision.gameObject.name.Equals("player")) && (colide == false))
{
//var created
colide = true;
heartscript.livesLost += 1;
Debug.Log(heartscript.livesLost);
//var created
colide = false;
}
}
I think this will make the action happens once
Hey thanks i tried this out but didn't work. Any more ideas will definitely be appreciated. Thanks for the help anyway
As I've said, it's not a script issue but something else affecting livesLost
And if you create a function in your heart script like this:
public void destroyHeart(){
if (livesLost == 1)
{
Destroy(heartOne);
}
else if (livesLost == 2)
{
Destroy(heartTwo);
}
else if (livesLost == 3)
{
Destroy(heartThree);
}
}
Then OnCollision:
if (collision.gameObject.name.Equals("player"))
{
heartscript.livesLost += 1;
heartscript.destroyHeart();
Debug.Log(heartscript.livesLost);
}
Remove from Update function this code part that we put in new Function and leave only the rotate
Follow this Question
Related Questions
OnCollisionEnter If statement error 1 Answer
Checking for collision of another gameObject with another trigger 1 Answer
Cannot get collision to do anything 1 Answer
OnCollisionEnter not working 2 Answers
get collision info from main script? 1 Answer