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 0mr_ashutosh0 · Nov 12, 2014 at 07:56 PM · functionscoreframeratefunction callscoreboard

No proper Update and function call between objects ?

TheCollision class detects collision and then calls score class to calculate score

 using UnityEngine;
 using System.Collections;
 
 public class TheCollision : MonoBehaviour {
 
     public Pool pool;
     public Poolb poolb;
 
     private Scores scores;
 
     void Start()
     {
         scores = GetComponent<Scores>();
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.tag == "moving")
         {
             poolb.deActivate(gameObject);
             pool.deActivate(other.gameObject);
 //            Debug.Log("Hitting the moving object");
             scores.AddScore();
         }
 
         if(other.tag == "still")
         {
             poolb.deActivate(gameObject);
 //            Debug.Log("Hitting the Still object");
             Application.Quit();
         }
     }
 
 }
 


Here is the score class

 using UnityEngine;
 using System.Collections;
 
 public class Scores : MonoBehaviour {
     
     public int totalScore;
     
     public void AddScore()
     {
         Debug.Log("AddScore called");
         totalScore = totalScore + 1;
         Debug.Log(totalScore);
     }
 
 }


alt text

Here is the problem After eliminating 10 enemies my score is still 6 but the function is called for exact 10 times

if function is called 10 times properly then why scores isnt getting updated ??

is it because of framerate ??

newprob.png (35.0 kB)
Comment
Add comment · Show 3
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 Bunny83 · Nov 12, 2014 at 08:04 PM 0
Share

Do you have any other code that might change the value of "totalScore"? $$anonymous$$aybe in another script? You might want to search for "totalScore" with the "Find In Files" search function in $$anonymous$$onoDevelop.

avatar image 0mr_ashutosh0 · Nov 12, 2014 at 08:25 PM 0
Share

hey @Bunny83

Here is this script which is using the totalScore but its just reading it and printing to canvas text

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DisplayScore : $$anonymous$$onoBehaviour {
 
     Text txt;
     private Scores scores;
 
     // Use this for initialization
     void Start () {
         txt = gameObject.GetComponent<Text>(); 
 
         GameObject ScoresObject = GameObject.FindGameObjectWithTag("manager");
         if(ScoresObject != null)
         {
             scores = ScoresObject.GetComponent<Scores>();
         }
     }
     
     // Update is called once per frame
     void Update () {
         txt.text="Score : " + scores.totalScore; 
     }
 }
 


BUT WHEN I ADDED $$anonymous$$Y 'SCORE' TO A E$$anonymous$$PTY GA$$anonymous$$E $$anonymous$$ANAGER AND $$anonymous$$ADE THESE CHANGES TO $$anonymous$$Y 'THECOLLISION' SCRIPT 'ALL $$anonymous$$Y ERRORS GONE' !!

 using UnityEngine;
 using System.Collections;
 
 public class TheCollision : $$anonymous$$onoBehaviour {
 
     public Pool pool;
     public Poolb poolb;
 
     private Scores scores;
 
     void Start()
     {
         GameObject ScoresObject = GameObject.FindGameObjectWithTag("manager");
         if(ScoresObject != null)
         {
             scores = ScoresObject.GetComponent<Scores>();
         }
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.tag == "moving")
         {
             poolb.deActivate(gameObject);
             pool.deActivate(other.gameObject);
 //            Debug.Log("Hitting the moving object");
             scores.AddScore();
         }
 
         if(other.tag == "still")
         {
             poolb.deActivate(gameObject);
 //            Debug.Log("Hitting the Still object");
             Application.Quit();
         }
     }
 
 }
 

BUT I HAVE NO IDEA WHY ?? :P

avatar image Bunny83 · Nov 12, 2014 at 08:49 PM 0
Share

Is it possible that you have (or had) multiple instances of your Scores script? You can add a reference to Debug.Log messages so when you click on the message the object you passed as reference will be highlighted in the hierarchy. So you might want to do this in the setter:

 Debug.Log("totalScore is being set to " + value + " was " + m_TotalScore + " before", this);

I'll bet that you have the Scores script on at least two objects ;)

1 Reply

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

Answer by Bunny83 · Nov 12, 2014 at 08:11 PM

The easiest way how to figure out when and where someone is reading / writing to totalScore is to convert it into a property ;)

 public class Scores : MonoBehaviour
 {
     private int m_TotalScore;
     public int totalScore
     {
         get { return m_TotalScore;}
         set
         {
             Debug.Log("totalScore is being set to " + value + " was " + m_TotalScore + " before");
             m_TotalScore = value;
         }
     }
     
     public void AddScore()
     {
         Debug.Log("AddScore called");
         totalScore = totalScore + 1;
         Debug.Log("Score after AddScore: " + totalScore);
     }
 }



The Debug.Log from the property should have a stacktrace, so you can see from where it's being called. You could also put a log in the getter if it's still not clear what's happening.

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

27 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

Related Questions

how to creat and scoring and high score system for an endless runner,how do I display my score on a game over scene and also create and high score system for the same ? 0 Answers

How to make an online scoreboard -1 Answers

How to save score for survival time? 1 Answer

Multiplayer ScoreBoard not Updating Correctly 0 Answers

first function dismiss 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