Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 05, 2017 at 06:03 PM by pako for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by manaslegodesigns · Dec 05, 2017 at 04:12 PM · oncollisionenter

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!!

Comment
Add comment · Show 2
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 ShadyProductions · Dec 05, 2017 at 04:22 PM 0
Share

Doesn't look like an issue in the script, are you sure nothing else is modifying the livesLost?

avatar image pako · Dec 05, 2017 at 06:02 PM 0
Share

@manaslegodesigns I cannot reproduce the problem. It's not the scripts.

3 Replies

  • Sort: 
avatar image
5
Best Answer

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);
     }
   }
 }
Comment
Add comment · Show 6 · 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
avatar image manaslegodesigns · Dec 06, 2017 at 10:42 AM 0
Share

Awesome dude, thanks i got it to work. I also rewarded u with some points. Thanks a lot

avatar image Saucyminator manaslegodesigns · Dec 06, 2017 at 10:55 AM 0
Share

Glad I could help!

avatar image pako manaslegodesigns · Dec 06, 2017 at 01:10 PM 0
Share

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.

avatar image Saucyminator pako · Dec 06, 2017 at 03:34 PM 0
Share

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.

Show more comments
avatar image meat5000 ♦ · Dec 08, 2017 at 01:26 PM 0
Share
 Debug.Log(other.gameObject.name);

The results will sometimes surprise you.

avatar image
2

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;
       }
 
 }
 

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
avatar image
0

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

Comment
Add comment · Show 3 · 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
avatar image manaslegodesigns · Dec 05, 2017 at 04:37 PM 0
Share

Hey thanks i tried this out but didn't work. Any more ideas will definitely be appreciated. Thanks for the help anyway

avatar image ShadyProductions manaslegodesigns · Dec 05, 2017 at 04:42 PM 0
Share

As I've said, it's not a script issue but something else affecting livesLost

avatar image Full8uster manaslegodesigns · Dec 05, 2017 at 05:19 PM 0
Share

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

Answers Answers and Comments

75 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

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


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