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 Arcticstar · Jun 23, 2017 at 09:39 PM · unity 5multiplayerupdatescoreinvokerepeating

multiply Score on Collision not working? check my code plz

So what I'm trying to do here is when the player collide By " x2" object the score gets update +2 instead of +1 , I tried a lot of codes and I can't get anything to work, maybe its easy and I miss something, please check my code and guide me here

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class UIManager : MonoBehaviour{

 public static bool  gameOver;
 
 public Text scoreText;
  int score;

 bool x2= false;


 // Use this for initialization
 void Start () {
     gameOver = false;
     score = 0;
     InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);


 }

 // Update is called once per frame
 void Update () {
 scoreText.text = "Score: " + score;


 }


 public void scoreUpdate() {

     if (gameOver == false) {
         if (x2 == false) {
             score += 1;
 
         }
         if (x2 == true) {

             score += 2;

         }
     }
 }


 public void gameover(){

     gameOver = true;
     
 }


 void OnTriggerEnter2D (Collider2D col) {

     if (col.gameObject.tag == "x2") {

         Destroy (col.gameObject);

         x2 = true;

         float timer = 5.0f;

         timer -= Time.deltaTime;

         if (timer <= 0) {
             x2 = false;
         }
     }
 }

}

the scores only update +1 and never multiply when collision is happens.

Comment
Add comment · Show 1
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 Guy_Yome · Jun 24, 2017 at 12:07 AM 0
Share
   public static bool  gameOver;
   
   public Text scoreText;
    int score;
   bool x2= false;
  float timer = 0f;
  
   // Use this for initialization
   void Start () {
       gameOver = false;
       score = 0;
       InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);
   }
   // Update is called once per frame
   void Update () {
   scoreText.text = "Score: " + score;
  
  timer -= Time.deltaTime;
  if (timer <= 0) {
       x2 = false;
       timer = 0f;
   }
   }
   public void scoreUpdate() {
       if (gameOver == false) {
           if (x2 == false) {
               score += 1;
   
           }
           if (x2 == true) {
               score += 2;
           }
       }
   }
   public void gameover(){
       gameOver = true;
       
   }
   void OnTriggerEnter2D (Collider2D col) {
       if (col.gameObject.tag == "x2") {
           Destroy (col.gameObject);
           x2 = true;
           timer = 5.0f;
       }
   }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cstooch · Jun 23, 2017 at 10:40 PM

I haven't been able to locate where your problem is, but I'm not sure what you're trying to accomplish with this though.. as far as I can tell this should really never do anything except if your frame is extremely slow. I'll try and step my way through the code in my head here, but I didn't see an issue. What I'd recommend YOU do though is put some Debug.Log outputs or something in your collision check though just to see if it's reaching that code (i.e. a collision is detected - make sure one object has a rigidbody if using collision)

 float timer = 5.0f;
          timer -= Time.deltaTime;
          if (timer <= 0) {
              x2 = false;
          }



Edit: yah I've looked through the logic, and I think it should work, I'd just highly recommend you put a Debug.Log right after where you set x2=true and see if that code is ever reached... if not, there's your issue (most likely you're doing something wrong with colliders).

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 Arcticstar · Jun 23, 2017 at 11:03 PM 0
Share

well the "x2" object gets destroyed when collides so it means it works and I did the Debug.log and the collision occurs fine. but it just keeps invoke repeating the first condition which is score +=1

avatar image cstooch Arcticstar · Jun 23, 2017 at 11:40 PM 0
Share

I tested a very watered down version of this and it worked. So I don't know. The logic looks right to me (aside from that one part of code which I pointed out which shouldn't do anything, but that isn't a problem either).

I think what I'd do next is put some Debug.Log at the very start of your scoreUpdate and print out gameOver as well as x2.

avatar image Arcticstar cstooch · Jun 23, 2017 at 11:55 PM 0
Share

I put a Debug.log after the if(x2 == true) to print (x2) and it does print true, it means it works, but the score is keep updating the same as +1. can you post the codes you tested?

Show more comments
avatar image Arcticstar · Jun 24, 2017 at 06:09 PM 0
Share

Thanks man, it turns out you were right, I used the script twice, but I had to make some changes, now it works perfectly.

avatar image
0

Answer by Guy_Yome · Jun 23, 2017 at 09:52 PM

If your collider for the 2x object is a trigger, it might not work. Or maybe you didnt assign the tag, just created it. Or you have a typo in the tag's name.

Comment
Add comment · Show 7 · 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 Guy_Yome · Jun 23, 2017 at 09:53 PM 0
Share

Also, you could have assigned the tag to the container of the object with the collider. $$anonymous$$ake sure the tag is on the one with it.

avatar image Arcticstar · Jun 23, 2017 at 10:04 PM 0
Share

thanks for the replay, I unchecked the trigger and changed the Ontrigger to Oncollision, but thats not the issue because the collision works fine as the "x2" object gets destroy when the player collides but the score +=2 never get activated.

avatar image Guy_Yome Arcticstar · Jun 23, 2017 at 11:51 PM 0
Share

Could you Debug.Log the gameover variable. $$anonymous$$aybe you are always at gameover =true.

avatar image Guy_Yome Guy_Yome · Jun 23, 2017 at 11:51 PM 0
Share

Also the time variable.

avatar image Guy_Yome Arcticstar · Jun 23, 2017 at 11:54 PM 0
Share

I think I know. The timer is initialized in the function. $$anonymous$$ake it a global variable. In the update you do the

timer -= time.deltaTime;

and if its 0 or under ( <= ) reset x2 to false and then make the timer = 0.

if (timer <= 0){ x2 = false; timer = 0; }

now when the collision enters make the timer 5.0f

timer = 5.0f;

avatar image Guy_Yome Guy_Yome · Jun 23, 2017 at 11:55 PM 0
Share

You check if its 0 or under in the update function too.

Show more comments

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

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

Related Questions

GUIText not updating score.... 1 Answer

UNET - UI Not updating with Healthbar and other bars. 1 Answer

Unity 5.2 Unet, extreme Lags after updating from 5.1 1 Answer

Is Unet too heavy for mobile games? 0 Answers

In lobby Manager i have overriden the GameObject OnLobbySeerverCreateGameplayer with 3 prefabs to be chosen by the player How to make each player choose his own character 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