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 /
avatar image
0
Question by scottfredeman · Aug 17, 2011 at 04:08 AM · scorecounter

Keeping Score registering more than one Point sometimes on Destroy

Hello,

Does anyone know of a way to force a score keeping script to register only one point when an object is destroyed, even if the object gets hit by multiple bullets at the same time? Sometimes when I shoot my Object that has my Keepscore script it Registers more than once when that object is destroyed. I am thinking that the object is not being destroyed fast enough sometimes because of a lower frame rate at that time and another bullet hits that same object and registers another point. I am not sure that's what's happening but it sounds logical.

Here is one of my scripts that has this problem:

Thank you in advance. Scott

function OnCollisionEnter(collision : Collision) {

 if (collision.gameObject.tag == "MISSILE")
 Destroy (gameObject);
 KEEPSCORE_SCRIPT.score++;
         

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by roamcel · Aug 17, 2011 at 05:13 AM

Since it's technically possible that the script gets triggered multiple times simultaneously (albeit I wouldn't think it to be that easy as it happens in your case), what you possibly just need is a simple flag variable.

 bool beenhitalready = false;

then your code would simply become

 if (collision.gameObject.tag == "MISSILE" && beenhitalready == false)
   beenhitalready = true;
   KEEPSCORE_SCRIPT.score++;
   Destroy (gameObject);
 }

If this works, you truly have a simple timing problem and there are other, more elegant solutions.

If it doesn't work, your problem is not a timing one.

Comment
Add comment · Show 1 · 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 ShaneTheVeryTall · Apr 08, 2013 at 10:11 PM 0
Share

Just curious, what would the more elegant solutions be? I came across this while googling my issue:

Every time a trigger is entered, I send a message to my game$$anonymous$$anager script to update the score:

 using UnityEngine;
 using System.Collections;
 
 public class TunaCan : $$anonymous$$onoBehaviour {
     
     public GameObject game$$anonymous$$gr;
     public int pointValue;
     public GameObject pickupAudio;
     
     void OnTriggerEnter() {
         Instantiate(pickupAudio, transform.position, transform.rotation);
         game$$anonymous$$gr.Send$$anonymous$$essage("UpdateScore", pointValue);
         iTween.ScaleBy(gameObject, iTween.Hash(
             "amount", Vector3.one * 2.0f,
             "time", 0.15f, 
             "easeType","easeOutCubic", 
             "oncomplete", "DestroySelf", 
             "oncompletetarget", gameObject));
     }
 
     void DestroySelf() {
         Destroy(gameObject);
     }
 }

Then in my game$$anonymous$$anager script:

 using UnityEngine;
 using System.Collections;
 
 public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
     
     public UILabel scoreText;
     public static int score;
     
     // Use this for initialization
     void Start () {
         score = 0;
         scoreText.text = score.ToString();
     }
     
     void UpdateScore(int pointValue) {
         score += pointValue;
         scoreText.text = score.ToString();    
     }
 }

Once in a while, if I hit two triggers close enough together, I get a higher score total than I should. I used the flag method you mentioned above, and that seemed to fix it. Just wondering if having that approach is somehow not optimal?

Thanks!

avatar image
0

Answer by scottfredeman · Aug 17, 2011 at 02:24 PM

Thank you for your reply. Appears it may not be a timing problem. What else can be causing this to happen if the Timing is not doing it? Thank you, Scott

Here is my complete script:

var explosion : GameObject;

var beenhitalready : boolean = false;

function OnCollisionEnter(collision : Collision) {

var contact : ContactPoint = collision.contacts[0];

var thisExplosion : GameObject = Instantiate (explosion, contact.point + (contact.normal * 5.0) , Quaternion.identity);

if (collision.gameObject.tag == "MISSILE" && beenhitalready == false)

beenhitalready = true;

Destroy (thisExplosion, 4.0);

Destroy (gameObject);

KEEPSCORE_INSTANTIATE_AND_PLAY_AUDIO_ANY_LEVEL.score++;
}

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to get score counter... 2 Answers

Scoring (i'm stuck) 3 Answers

Make a custom score counter in unity with c# 1 Answer

how to keep track of bricks broken 1 Answer

On Trigger Enter just triggers once for same object 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