Search Unity

My function is not returning value, Please Help asap

Discussion in 'Scripting' started by vahi2008, Apr 15, 2013.

  1. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    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
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scoreHandler : MonoBehaviour {
    5.  
    6.    
    7.     void Start(){
    8.         LoadScore();
    9.     }
    10.    
    11.     //public static GameObject targetText2;
    12.    
    13.    
    14.     // STATIC
    15.         public static GUIText scoreText = null;
    16.     public static GUIText highscoreText = null;
    17.     public static GUIText totalBottles = null;
    18.     public static int _highscore;
    19.    
    20.         static int _currentGameScore;
    21.     static int _currentgameBottlesCollected;
    22.    
    23.     public static GUIText bottleText = null;
    24.    
    25.     //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
    26.         public static int currentGameScore {
    27.                 get { return _currentGameScore; }
    28.                 set {
    29.             Debug.Log("scoretext-"+scoreText);
    30.                         if (value!=_currentGameScore  scoreText!=null) {
    31.                 //value here means -currentGameScore
    32.                                 _currentGameScore = value;
    33.                                 scoreText.text = value.ToString();
    34.                 //Debug.Log("Bottles"+currentGameScore);
    35.                 _highscore = _highscore + currentGameScore;
    36.                 highscoreText.text = _highscore.ToString();
    37.                 SaveScore();
    38.                 // 1. it should read the highscore from playerprefs and then keep on adding _currentGameScore to it and saving it
    39.                 // 2. show the highscore in guiText highScore
    40.            
    41.                         }
    42.                 }
    43.         }
    44.    
    45.    
    46.     public static int currentgameBottlesCollected {
    47.         get { return _currentgameBottlesCollected; }
    48.         set {
    49.             Debug.Log("bottletext-"+bottleText);
    50.               if(value != _currentgameBottlesCollected  bottleText!=null) {
    51.             _currentgameBottlesCollected = value;
    52.                 bottleText.text = value.ToString();
    53.                 Debug.Log("Bottles"+currentgameBottlesCollected);
    54.             }
    55.     }
    56.     }
    57.    
    58.    
    59.    
    60.                
    61.    
    62.      // INSTANCE
    63.     public GUIText targetText1;
    64.     public GUIText highScore;
    65.    
    66.      void Awake() {
    67.                 scoreText = targetText1;
    68.         highscoreText = highScore;
    69.        
    70.        
    71.        
    72.         }
    73.    
    74.    
    75.     public static void SaveScore(){
    76. PlayerPrefs.SetInt("highscoreText", _highscore);
    77. }
    78.    
    79.     public static void LoadScore(){
    80. _highscore = PlayerPrefs.GetInt("highscoreText",0);
    81. }
    82.    
    83.    
    84.  
    85. }
    86.  
    87.  
    88.  
    bottleMovement.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public enum BottleDirection
    5. {
    6.         NONE = 0,
    7.         LEFT = -1,
    8.         RIGHT = 1
    9. }
    10.  
    11. [System.Serializable]
    12. public class bottleMovement : MonoBehaviour
    13. {
    14.         public Vector2 speed;
    15.     public static int singleGameScore = 0;
    16.     //public GameObject targetText;
    17.    
    18.    
    19.    
    20.  
    21.    
    22.    
    23.         public BottleDirection direction;
    24.  
    25.         //called every physics step
    26.         public void FixedUpdate()
    27.         {
    28.                 MoveBottle();
    29.                 CheckForDisappearance();
    30.         }
    31.  
    32.         //called when collisions happen
    33.         public void OnCollisionEnter( Collision col )
    34.         {
    35.        
    36.                 //do stuff here
    37.         Debug.Log ("collided");
    38.         Debug.Log(col.gameObject.tag);
    39.        
    40.         if(col.gameObject.tag == "drunk"){
    41.             singleGameScore = singleGameScore + 1;
    42.             scoreHandler.currentGameScore = singleGameScore;
    43.            
    44.             GameObject.Destroy(col.gameObject);}
    45.         else if(col.gameObject.tag == "nondrunk"){
    46.             singleGameScore = singleGameScore - 1;
    47.             scoreHandler.currentGameScore = singleGameScore;
    48.             GameObject.Destroy(col.gameObject);
    49.             }
    50.    
    51.        
    52.        
    53.         //Destroy(gameObject);
    54.         }
    55.  
    56.         private void MoveBottle()
    57.         {
    58.                 //movement code, use this speed:
    59.                 Vector3 useSpeed = new Vector3(speed.x * (int)direction, 0, speed.y);
    60.        
    61.         transform.Translate(useSpeed * Time.deltaTime);
    62.                
    63.         }
    64.  
    65.         private void CheckForDisappearance()
    66.         {
    67.            
    68.                 //check if it's out the screen and delete
    69.         }
    70.       void OnBecameInvisible() {
    71.         Destroy(gameObject);
    72.     }
    73. }


    bottleCollector.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bottleCollector : MonoBehaviour {
    5.    
    6.     public static int _currentBottlesCollected=0;
    7.    
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15. //  void FixedUpdate () {
    16. //  transform.Translate(-Vector3.forward * Time.deltaTime);
    17. //      if(characterScript.hero_z_position>transform.position.z) Destroy(gameObject);
    18. //  }
    19.  
    20.     void OnTriggerEnter(Collider other) {
    21.         if(other.gameObject.tag == "Player"){
    22.             _currentBottlesCollected=_currentBottlesCollected+1;
    23.             scoreHandler.currentgameBottlesCollected = _currentBottlesCollected;
    24.             Debug.Log(scoreHandler.currentgameBottlesCollected);
    25.             Debug.Log(".."+_currentBottlesCollected);
    26.             Destroy(gameObject);
    27.     }}
    28.    
    29.      
    30.    
    31.    
    32.    
    33.    
    34. }
    35.  
     
    Last edited: Apr 15, 2013
  2. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    problem solved. I did not called bottleText in awake