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
1
Question by Zerodegrees · Feb 10, 2013 at 06:12 AM · if-statementscollision issuescounting

Problem with Counting Collisions

Very simply I'm doing the Beginner Challenge 3, and I'm pretty sure I got everything down the only problem is this weird bug I can't figure out. I have it so when my sphere is launched and hits a box my score updates by adding one to score. This all works just fine...the first time.

Afterwards it never updates, and it seems it's only the "Score++;" that doesn't work anymore after the first "hit". I've done Debug.Log messages to see if it's even running through the if logic and it does, it displays every time I hit the cubes again, it just doesn't add one to score. I also checked if it's a cube problem, but the same thing happens on each cube when their first hit.

I've also tried using "Score = Score + 1;", "Score+=1;" to no avail. Lastly, I've checked the GUIText and it can change just fine if I have it display something different from hitting the floor to a cube, so I'm pretty sure it's just the Score++; part that isn't working right.

TL;DR: A Score++; counting variable for counting sphere to cube collisions isn't incrementing after the first hit.

Update: I got around this by creating another script that updates the score by accessing another script on the GUIText. Basically what's done here:http://answers.unity3d.com/questions/200262/changing-variables-in-another-script.html

Code:

var Score : int = 0;

function OnCollisionEnter(hit : Collision) {

 var myText : GUIText = gameObject.Find("GUI Text").GetComponent(GUIText);
 
 if(hit.gameObject.name == "cCube")
 {
     //(I added a Debug.Log message here to check the value of Score, it seems to reset to 0 here everytime whether or not the variable is declared outside or inside the function.)
     Score++;
     myText.text = "Score: " + Score;
     
     
     Destroy(gameObject.Find("FireCannon(Clone)"));
 
 }
 else if(hit.gameObject.name == "bCube")
 {
     Score++;
     myText.text = "Score: " + Score;
     
     Destroy(gameObject.Find("FireCannon(Clone)"));
 }
 else if(hit.gameObject.name == "aCube")
 {
     Score++;
     myText.text = "Score: " + Score;
     
     Destroy(gameObject.Find("FireCannon(Clone)"));    
 }
 
     

}

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 iwaldrop · Feb 10, 2013 at 06:22 AM

From what I see of you code Score will always equal 1 at the end of OnCollisionEnter. This is because you declare the variable within the method. If you want to use it as a counter between OnCollisionEnter calls then you need to declare it as a member variable; outside of the scope of your method. ;)

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 Zerodegrees · Feb 10, 2013 at 05:18 PM

Didn't work, I should have put that I tried that already. I've tried putting the declaration both inside the function and outside at the beginning like normal. This code was just when the last time I had it inside. It also isn't resetting to 0 either, it starts at 0, I hit a block it goes to 1 then any time after that it just stays at 1. I can hit any block and it won't go up or down or anything. I've tried setting the floor to work the same way where it adds 1 when it hits the floor and the same thing happens.

(In response to iwaldrop, still new to the forums)

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

10 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

Related Questions

Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers

Unity freezes after running this basic code. 2 Answers

How to call unity event once every time my object reach destination in Update()? 1 Answer

Is there any way to upscale one variable if another one goes up? (C#) 1 Answer

OnCollisionEnter2D is called but the actually collision isn't resolving. 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