- Home /
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);
}
}
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 ??
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.
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
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 ;)
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.
Your answer
Follow this Question
Related Questions
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