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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by BaldiWonKenobi · Aug 07, 2014 at 06:05 PM · collisiongameobjectscorescore system

game issues with infinte collisions

ok so here is what I am trying to do... I am making a game where you throw objects at a board. If the objects hit the board you get a plus 1 to the score. if the objects gets knocked off you lose that point. I figured the score would be a simple thing to do if the bag and board collide increment the score by 1... the issue is that it never stops incrementing the score when the collision happens.. I have tried using both onTriggerEnter and onCollisionEnter and neither one increments by just one. So I threw a Boolean function in so if it is true it will increment the score by one as well it also did not work... so now after two weeks of a bajillion different code variations at doing this I am reaching out to you guys for some assistance... can I get a working example of how to do this... I understand the unwillingness to just give answers... but I have been fighting with this for two weeks now and feel like I have all the pieces I am just not putting them in the right order.... I have not posted the code because I feel like I need to start over with it, hopefully with a better understanding provided by your responses. Thanks in advance.... Richard

Comment
Add comment · Show 7
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 ♦ · Aug 07, 2014 at 06:08 PM 0
Share

onTriggerEnter, onCollisionEnter?

OnTriggerEnter, OnCollisionEnter :)

Guessing thats not really your problem, I just thought I'd mention it.

avatar image BaldiWonKenobi · Aug 07, 2014 at 07:00 PM 0
Share

ok here is my code if it will help it might be more confusing lol...

 // this code is on the board
 using UnityEngine;
 using System.Collections;
 
 public class ID355 : $$anonymous$$onoBehaviour 
 {
     public GameObject score; //reference to the ScoreText gameobject, set in editor
     public GameObject otherObject;
     public bool okToScore = true;
     public int redScore; 
     public int redTurn;
     public int scoreRed = 0;
     public GameObject isTouched;
 
     void OnTriggerEnter(Collider  col) {
 
         bool touch$$anonymous$$e = isTouched.GetComponent<floorPlane> ().isTouched;
         if (col.gameObject.tag == "bagCloneRed" && okToScore == false && touch$$anonymous$$e == false) {
             otherObject.GetComponent<bagDetect> ().canScore = false;
 
             okToScore = true;
             redTurn = redTurn +1;
             otherObject.GetComponent<bagDetect> ().canScore = false;
         }
         if (col.gameObject.tag == "bagCloneRed" && okToScore == false && touch$$anonymous$$e == true) {
             otherObject.GetComponent<bagDetect> ().canScore = false;
             
             okToScore = false;
             redTurn = redTurn +1;
             otherObject.GetComponent<bagDetect> ().canScore = true;
         }
 }
     void Update (){
         if (redTurn == 4) {
             print (scoreRed);
             int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + scoreRed; //add 1 to the score
             score.GetComponent<GUIText> ().text = currentScore.ToString ();
             redTurn = 0;
             scoreRed = 0;
         }
     }

}

avatar image BaldiWonKenobi · Aug 07, 2014 at 07:17 PM 0
Share

and here is the rest well for some reason it is not letting me post the rest

avatar image BaldiWonKenobi · Aug 07, 2014 at 07:21 PM 0
Share
 //this code is on on the bag 
         
         
         
         
         using UnityEngine;
         using System.Collections;
         
         public class bagDetect : $$anonymous$$onoBehaviour {
             public GameObject okToShoot;
             public bool canScore;
             public GameObject score;
             public GameObject scoreRed;
             public GameObject isTouched;
         
         void Start() {
         
                 if (okToShoot.GetComponent<ID355> ().okToScore == true) {
                     okToShoot.GetComponent<ID355> ().okToScore = false;
                     scoreRed.GetComponent<ID355> ().scoreRed = scoreRed.GetComponent<ID355> ().scoreRed +1;
                 }
         
         
             }
         void OnCollisionEnter(Collision col){
             
                 if (col.gameObject.tag == "floorPlane") {
                     isTouched.GetComponent<floorPlane> ().isTouched = true;
                 }
         }
                 
         }
         
         
avatar image BaldiWonKenobi · Aug 07, 2014 at 07:21 PM 0
Share

//on the floor

 using UnityEngine;
 using System.Collections;
 
 public class floorPlane : $$anonymous$$onoBehaviour {
 
     public bool isTouched;
 
     // Use this for initialization
     void Start () {
         isTouched = false;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 07, 2014 at 06:05 PM

Posting code is always a good idea, even when you feel you have to start over. It can tells us many things: what language you write in, the data structures you use, the relationship between game object (where you store the score for example in this probelm), etc.

I suspect your problem is that you are not check what you are colliding with. That would be that later hits by other projectiles might directly or indirectly cause OnCollisionEnter() calls resulting in score additions. Here is the bones of a script that would go on the projectiles. It expect the board to have the name 'Board', and the box or plane under the board, to be named 'Floor'. Note the way this logic is structured, if the projectile first hits the board and then falls to the floor (or is knocked to the floor by another projectile), the score for that projectile would 0.

     #pragma strict

     bool hitboard = false;
     bool hitfloor = false;

     function OnCollisionEnter(col : Collision) {
             if (!hitboard && col.gameObject.name == "Board") {
                     // Do whatever to add one to score
                     hitboard = true;
             }

             if (!hitfloor && col.gameObject.name == "Floor") {
                     // Do whatever to subtract one from score
                     hitfloor = true;
                     hitboard = true;
             }
     }
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 BaldiWonKenobi · Aug 07, 2014 at 07:23 PM 0
Share

I posted the code if you want to look at it

avatar image BaldiWonKenobi · Aug 07, 2014 at 07:34 PM 0
Share

robertbu I will try that out and see how it works... it could be I have just over engineered the code

avatar image BaldiWonKenobi · Aug 07, 2014 at 11:15 PM 0
Share

robertbu thanks for the help this answered the issue... i most defninately over engineered my code lol

pragma strict

 bool hitboard = false;
 bool hitfloor = false;
 
 function OnCollisionEnter(col : Collision) {
         if (!hitboard && col.gameObject.name == "Board") {
                 // Do whatever to add one to score
                 hitboard = true;
         }
 
         if (!hitfloor && col.gameObject.name == "Floor") {
                 // Do whatever to subtract one from score
                 hitfloor = true;
                 hitboard = true;
         }
 }
avatar image
0

Answer by Foose · Aug 07, 2014 at 06:08 PM

I don't know in which manner you are using the Physics engine, but if the object really can be knockbacked from the board you could simply add an OnCollisionExit function to your collision script. If the two objects dont collide anymore do "score - 1" or sth. would be the easiest way i guess

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 · Aug 07, 2014 at 07:23 PM 0
Share

I posted the code if you want to look at it

avatar image BaldiWonKenobi · Aug 07, 2014 at 07:33 PM 0
Share

Foose that makes sense but the score still continuously counts up while the object is touching it... in this case the board is at a very slight angle on the ground so the object a small beanbag in this case sits on it.

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

How to add the right amount of points when my player destroys a gameobject? 0 Answers

Scoring zones work, but need score to be cumulative. 1 Answer

How to move a Game Object from a script not attached to it. 1 Answer

I want my score to reset back to 0 but keep my highscore saved 3 Answers

I need my script to load and save my players score to a text file . When I attach the script to a gameobject the script either is not found or it just prints 5,4,3,2,1 or all )0's. 0 Answers


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