I have a script called scoreHandler.cs , it has two functions, In 1 function it is returning value from bottleMovement.cs and in the 2nd function it is returning value from bottleCollector.cs
Ist function is working fine, where 2 nd function is not.
I think problem is due to this -
In scoreHandler.cs Line no. 49 Debug.Log(“bottletext-”+bottleText); is always coming null and
In bottleCollector.cs Line no. 24 Debug.Log(scoreHandler.currentgameBottlesCollected); is always coming zero
1. Why scoreText is not coming null and why bottleText is coming null always ?
2. Why scoreHandler.currentgameBottlesCollected is always coming zero ?
Due to this i am unable to go inside the if condition line 50 scoreHandler.cs
What is wrong, please help me
I am copying all the 3 scripts here, please help me, what is going wrong with it.
scoreHandler.cs
using UnityEngine;
using System.Collections;
public class scoreHandler : MonoBehaviour {
void Start(){
LoadScore();
}
//public static GameObject targetText2;
// STATIC
public static GUIText scoreText = null;
public static GUIText highscoreText = null;
public static GUIText totalBottles = null;
public static int _highscore;
static int _currentGameScore;
static int _currentgameBottlesCollected;
public static GUIText bottleText = null;
//When we assign to currentGameScore (the property), it runs the set{} block, it checks if 'value' (which is the incoming value) is different from the real internal _currentGameScore - if they are the same it won't do anything. If they are different it will update the internal value and do a ToString on it, then send that to the GUIText
public static int currentGameScore {
get { return _currentGameScore; }
set {
Debug.Log("scoretext-"+scoreText);
if (value!=_currentGameScore scoreText!=null) {
//value here means -currentGameScore
_currentGameScore = value;
scoreText.text = value.ToString();
//Debug.Log("Bottles"+currentGameScore);
_highscore = _highscore + currentGameScore;
highscoreText.text = _highscore.ToString();
SaveScore();
// 1. it should read the highscore from playerprefs and then keep on adding _currentGameScore to it and saving it
// 2. show the highscore in guiText highScore
}
}
}
public static int currentgameBottlesCollected {
get { return _currentgameBottlesCollected; }
set {
Debug.Log("bottletext-"+bottleText);
if(value != _currentgameBottlesCollected bottleText!=null) {
_currentgameBottlesCollected = value;
bottleText.text = value.ToString();
Debug.Log("Bottles"+currentgameBottlesCollected);
}
}
}
// INSTANCE
public GUIText targetText1;
public GUIText highScore;
void Awake() {
scoreText = targetText1;
highscoreText = highScore;
}
public static void SaveScore(){
PlayerPrefs.SetInt("highscoreText", _highscore);
}
public static void LoadScore(){
_highscore = PlayerPrefs.GetInt("highscoreText",0);
}
}
bottleMovement.cs
using UnityEngine;
using System.Collections;
public enum BottleDirection
{
NONE = 0,
LEFT = -1,
RIGHT = 1
}
[System.Serializable]
public class bottleMovement : MonoBehaviour
{
public Vector2 speed;
public static int singleGameScore = 0;
//public GameObject targetText;
public BottleDirection direction;
//called every physics step
public void FixedUpdate()
{
MoveBottle();
CheckForDisappearance();
}
//called when collisions happen
public void OnCollisionEnter( Collision col )
{
//do stuff here
Debug.Log ("collided");
Debug.Log(col.gameObject.tag);
if(col.gameObject.tag == "drunk"){
singleGameScore = singleGameScore + 1;
scoreHandler.currentGameScore = singleGameScore;
GameObject.Destroy(col.gameObject);}
else if(col.gameObject.tag == "nondrunk"){
singleGameScore = singleGameScore - 1;
scoreHandler.currentGameScore = singleGameScore;
GameObject.Destroy(col.gameObject);
}
//Destroy(gameObject);
}
private void MoveBottle()
{
//movement code, use this speed:
Vector3 useSpeed = new Vector3(speed.x * (int)direction, 0, speed.y);
transform.Translate(useSpeed * Time.deltaTime);
}
private void CheckForDisappearance()
{
//check if it's out the screen and delete
}
void OnBecameInvisible() {
Destroy(gameObject);
}
}
bottleCollector.cs
using UnityEngine;
using System.Collections;
public class bottleCollector : MonoBehaviour {
public static int _currentBottlesCollected=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
// void FixedUpdate () {
// transform.Translate(-Vector3.forward * Time.deltaTime);
// if(characterScript.hero_z_position>transform.position.z) Destroy(gameObject);
// }
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Player"){
_currentBottlesCollected=_currentBottlesCollected+1;
scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
Debug.Log(scoreHandler.currentgameBottlesCollected);
Debug.Log(".."+_currentBottlesCollected);
Destroy(gameObject);
}}
}