Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BaldiWonKenobi · Jul 28, 2014 at 03:59 PM · c#collisionscore

Multiple scores from collisions how do I stop after one collsion

I am creating a game that you throw objects at a board and if the object lands on the board you get a score. The problem is when the object hits the board sometimes it bounces or rolls and multiple scores are logged. I would like for only one score to be annotated per throw. Thanks in advance and sorry if this question has been asked before... I looked and could not really find anything similar. Here is the code:`using UnityEngine; using System.Collections;

public class ID355 : MonoBehaviour { public GameObject score; //reference to the ScoreText gameobject, set in editor public GameObject otherObject;

     void OnCollisionEnter() //if bag hits board
     {
     otherObject.GetComponent<bagDetect>().bagHit = false;
     bool hit = otherObject.GetComponent<bagDetect>().bagHit;
         if (hit == false) 
         {
             int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 1; //add 1 to the score
             score.GetComponent<GUIText> ().text = currentScore.ToString ();
             otherObject.GetComponent<bagDetect>().bagHit = true;
             print(hit);
         }
             
     }
     
     
     void OnTriggerEnter() //if bag hits collider
     {
         
             int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 3; //add 3 to the score
             score.GetComponent<GUIText> ().text = currentScore.ToString ();
     }
 }



` and here is the code for otherObject:

 using UnityEngine;
 using System.Collections;
 
 public class bagDetect : MonoBehaviour {
     public bool bagHit = false;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

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 meat5000 ♦ · Jul 28, 2014 at 03:31 PM 0
Share

$$anonymous$$ake some kind of oneshot boolean to switch off the scoring after the first one, or add a time delay before any scoring can take place after a score has been logged.

avatar image BaldiWonKenobi · Jul 28, 2014 at 04:40 PM 0
Share

that is kinda what I tried doing but the problem is that there will be four objects thrown and each of the objects is a prefab and when I do it that way it scores the first one but no others after it... so I don't know what the best course of action for a solution would be... maybe make four separate objects ins$$anonymous$$d of prefab? or an array of some sort

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by 432qwerty · Jul 28, 2014 at 04:47 PM

You can either:

  1. Have a one-shot boolean that stops the score retriggering, as @meat5000 suggested

  2. Use void OnCollisionStay() instead of void OnCollisionEnter() if you have this problem every time a bag hits the board.

I would say the one-shot bool is probably your best option.

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 DiGiaCom-Tech · Jul 28, 2014 at 06:02 PM

Your prefabs become unique objects when instantiated. So you need a flag in the object being thrown prefab code like 'IsScored' initially set to false.

Then on collision with the board you check the state of the flag to see if the object was scored. It not scored (e.g. if (!IsScored) then...) then increment the score and set the objects 'IsScored' to true. Subsequent collisions of the thrown object will not increment the score.

Note, this scores the first time the object hits the board. So, if it hits the ground (or something else) first and then hits the board is not a score, then you'll need to set it on any collision vs. just the board collision.

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 BaldiWonKenobi · Jul 28, 2014 at 06:50 PM

DiaGiaCom," Your prefabs become unique objects when instantiated. So you need a flag in the object being thrown prefab code like 'IsScored' initially set to false.

Then on collision with the board you check the state of the flag to see if the object was scored. It not scored (e.g. if (!IsScored) then...) then increment the score and set the objects 'IsScored' to true. Subsequent collisions of the thrown object will not increment the score."

this is what I did... with the otherObject (otherObject is the code on the prefab)and the first code listed... I understand what you are saying I think I am missing something small with this ....

Comment
Add comment · Show 2 · 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 BaldiWonKenobi · Jul 28, 2014 at 07:02 PM 0
Share

So maybe something like this? void OnCollisionEnter() //if bag hits board { bool currentScore = true; if (currentScore == true) { int.Parse (score.GetComponent ().text) + 1; //add 1 to the score score.GetComponent ().text = currentScore.ToString (); } }

avatar image BaldiWonKenobi · Jul 29, 2014 at 12:17 AM 0
Share

I tried this:

   public GameObject score; //reference to the ScoreText gameobject, set in editor
         public GameObject otherObject;
         
         void OnCollisionEnter() //if ball hits board
         {
             otherObject.GetComponent<bagDetect>().bagHit = false;
             //bool hit = otherObject.GetComponent<bagDetect>().bagHit;
             if (otherObject.GetComponent<bagDetect>().bagHit != true) 
             {
                 int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + 1; //add 1 to the score
                 score.GetComponent<GUIText> ().text = currentScore.ToString ();
                 otherObject.GetComponent<bagDetect>().bagHit = true;
                 print(otherObject.GetComponent<bagDetect>().bagHit);
             }
             
         }
         
     

but it did not work either even though the boolean on otherObject was changed to true? any thoughts?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

how to keep track of bricks broken 1 Answer

Most efficient way to get scores 1 Answer

Multiple Cars not working 1 Answer

Trying to get this coin system to work to no avail 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