Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

doing uGui Score system. problemsss :(

Discussion in 'Scripting' started by AppleJuicy, Feb 6, 2015.

  1. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    below are 2 linked scripts. i wanted to make cash uGui cash to run. it stuck at "cash: 0" all the time and not running~ :(

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

    public class Scoring : MonoBehaviour {

    public static int cash;
    public static int penalty;

    public static Text cashText;
    public static Text penaltyText;

    void start()
    {
    cash = 0;
    penalty = 0;
    cashText = GameObject.Find ("CashText").GetComponent<Text> ();
    penaltyText = GameObject.Find ("PenaltyText").GetComponent<Text> ();

    }
    }



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

    public class Destroy : MonoBehaviour {

    public GameObject other;

    void OnTriggerEnter2D (Collider2D other)
    {
    if(other.gameObject.tag == "Player")
    {
    Destroy (gameObject); //destroy object

    Scoring.cashText.text = ("Score: " + (Scoring.cash + 50));

    if (Scoring.cash <= 0)
    {
    Scoring.cash = 0;

    }
    }
    }
    }
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Try putting a Debug.Log(); call in OnTriggerEnter2D to see if it is firing.

    Also, please use Code Tags. People usually just skip threads with hard to read code like you have there.
     
    AppleJuicy and Kiwasi like this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    You've destroyed the object before setting the text.
     
    IchBinJager likes this.
  4. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    hm... i still don't get it. i want the object to be destroy , then score text + 50 point. i attached an error i got below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Destroy : MonoBehaviour {
    6.  
    7. public GameObject other;
    8.  
    9. void OnTriggerEnter2D (Collider2D other)
    10. {
    11. if(other.gameObject.tag == "Player")
    12. {
    13. Destroy (gameObject); //destroy object
    14.  
    15. Scoring.cashText.text = ("Score: " + (Scoring.cash + 50)); // <----- is where the error i attached occur
    16.  
    17. if (Scoring.cash <= 0)
    18. {
    19. Scoring.cash = 0;
    20.  
    21. }
    22. }
    23. }
    24. }
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      97.5 KB
      Views:
      703
  5. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    i have added a debug.log on line 14. it works.
    and added debug.log on line 16. it doesnt work.

    actually i worked on OnGUI before. it work fine. however font size cannot be change.
    so now i trying uGUI it doesn't work =(
     
  6. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Try moving the Destroy(gameObject) call to after the if (Scoring.cash <= 0) { } statement.
     
    AppleJuicy and Tomnnn like this.
  7. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    As @tango209 says, the script stops executing when the object is destroyed. If you destroy it then nothing after it will be run.
     
  8. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    it doesnt destroy the object anymore after doing so wondering why =\
     
  9. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    ive tried to put debug.log after the destroy(gameobject) earlier. debug.log is working.
    is it the script below got some problem?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Scoring : MonoBehaviour {
    6.  
    7. public static int cash;
    8. public static int penalty;
    9.  
    10. public static Text cashText;
    11. public static Text penaltyText;
    12.  
    13. void start()
    14. {
    15. cash = 0;
    16. penalty = 0;
    17. cashText = GameObject.Find ("CashText").GetComponent<Text> ();
    18. penaltyText = GameObject.Find ("PenaltyText").GetComponent<Text> ();
    19.  
    20. }
    21. }
    22.  
    For Easier reference i re-paste the above.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class Destroy : MonoBehaviour {
    5. public GameObject other;
    6. void OnTriggerEnter2D (Collider2D other)
    7. {
    8. if(other.gameObject.tag == "Player")
    9. {
    10. Destroy (gameObject); //destroy object
    11. Scoring.cashText.text = ("Score: " + (Scoring.cash + 50)); // <----- is where the error i attached occur
    12. if (Scoring.cash <= 0)
    13. {
    14. Scoring.cash = 0;
    15. }
    16. }
    17. }
    18. }
     
    Last edited: Feb 7, 2015
  10. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    I suggest you learn how to use the debugger (see the link in my signature) and place a breakpoint on line 11 (Scoring.cashText....) and see what is showing up as null.

    Also, it seems you didn't move the Destroy(gameObject); call. Move it to after line 15. It should be the last thing you do in the OnTriggerEnter2D() method.
     
    AppleJuicy likes this.
  11. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    is it like this? after moving the Destroy( gameObject); to the last part, Destroy(gameObject); is not working in Play Mode anymore. and i think debugger is wrong. it doesnt show anything.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Destroy : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     void OnTriggerEnter2D (Collider2D other)
    10.     {
    11.     if (other.gameObject.tag == "Player") {
    12.  
    13.    Scoring.cashText.text = ("Score:" + (Scoring.cash + 50));
    14.  
    15.    Debug.Log (string.Format ("Cash:{0}", Scoring.cashText.text));    <--- not very sure =<
    16.  
    17.    Destroy (gameObject);    <---- doesnt work when i place here.
    18.  
    19.    }
    20.    }
    21.    }
    22.  
     
  12. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    If the Debug.Log() does not produce output, then something blew up before it and the program bailed out of the method. If you don't use the debugger to set a breakpoint on the if statement and step through the code to find the problem then you will have to do it old school and place a Debug.Log before the if statement, before the Scoring.cashText.text... also and see which one produces output.

    So, looking back at the error and all, something on that Scoring line is not set. Try this as the first lines in the if statement and see if it can print all of those out with expected values:

    Code (CSharp):
    1. Debug.Log(Scoring);
    2. Debug.Log(Scoring.cashText);
    3. Debug.Log(Scoring.cash);
     
    AppleJuicy likes this.
  13. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Destroy : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     void OnTriggerEnter2D (Collider2D other)
    10.     { //remove the object from gameplay
    11.             if (other.gameObject.tag == "Player") {  //if the object tag is money
    12.  //           Debug.Log (Scoring);  <----Untitled3.jpg
    13. //            Debug.Log (Scoring.cashText); <----Untitled2.jpg
    14. //            Debug.Log(Scoring.cash); <----Untitled1.jpg
    15.          
    16.  
    17.  
    18.                     Scoring.cashText.text = ("Score:" + (Scoring.cash + 50));
    19.                     Debug.Log (string.Format ("Cash:{0}", Scoring.cashText.text));
    20.                     Destroy (gameObject);
    21.             }
    22.         }
    23.     }
    24.  
     

    Attached Files:

  14. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    cashText has not been set and that is why it is throwing the NULLReferenceException in Untitled2.jpg. Is cashText a Text variable on the object Scoring? If so, look in the inspector and see if you have that variable assigned to or if you did it in code check the assignment there.
     
    AppleJuicy likes this.
  15. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    this script got some error. i am sure the cashText has some problem with it. but i couldnt find. hm... there is nothing i need to assign on the script, or the object. i followed this tutorial ---->

    why he can i cant >.<
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Scoring : MonoBehaviour {
    6.  
    7. public static int cash;
    8. public static int penalty;
    9.  
    10. Text cashText;
    11. Text penaltyText;
    12.  
    13.     void start()
    14.         {
    15.         cashText = GameObject.Find("CashText").GetComponent<Text> ();
    16.         penaltyText = GameObject.Find ("PenaltyText").GetComponent<Text> ();
    17.         cash = 0;
    18.         penalty = 0;
    19.        }
    20.  
    21. void Update(){
    22. cashText.text = "Score:" + cash;
    23. }
    24. }

    I think this script working fine now.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Destroy : MonoBehaviour {
    6.     void OnTriggerEnter2D (Collider2D other)
    7.     { //remove the object from gameplay
    8.             if (other.gameObject.tag == "Player") {  //if the object tag is money
    9.             Scoring.cash += 50;
    10.             Debug.Log(Scoring.cash);  
    11.             Destroy (gameObject);
    12.             }
    13.         }
    14.     }
     
    Last edited: Feb 8, 2015
  16. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    C# is case sensitive, your start method isn't being called, change it to "void Start()".
     
    AppleJuicy likes this.
  17. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    MAN I WANNA STAB MYSELF WITH THAT its working fine now >.<
    sorry to take up ur time for this stupid mistake.
    thanks alot
     
  18. AppleJuicy

    AppleJuicy

    Joined:
    Nov 12, 2014
    Posts:
    31
    anyway i learned tht debug could help in some ways. thank:p
     
  19. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    No worries, it's always the simple things and yes learning to debug is one of, if not the, most important skill you will need in developing.