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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to pick up items and show number count in GUIText

Discussion in 'Scripting' started by jacky1234, Apr 5, 2015.

  1. jacky1234

    jacky1234

    Joined:
    Apr 5, 2015
    Posts:
    2
    Hi guys, I have a question in picking up items and showing the count in GUIText, I have created some "Sticks" to pick up and a Stick Counter, I also added a sphere collider under "Sticks" to be picked up at a certain distance. I added the tag "Stick" for the sticks, and a script as below:

    Code (csharp):
    1. private var stick : int = 0;
    2. var StickCounter : GUIText;
    3. private var player : GameObject;
    4.  
    5. function OnTriggerEnter (other : Collider) {
    6.  
    7. if(other.gameObject == player)
    8. {
    9. playerInRange = true;
    10. }
    11. if (this.tag == "Stick"){
    12. stick += 1;
    13. Destroy(gameObject,0);
    14. }
    15. }
    16.  
    17. function Update () {
    18. }
    19.  
    20. function OnGUI ()
    21. {
    22. StickCounter.text = stick.ToString();
    23. }
    The "Stick" disappeared when I passed through it but the GuiText number counter doesn't change, I'm not sure why it's not working, I'm thinking maybe because of Collider thing....Would someone please help? I totally have no idea how to fix this, Big Thanks!
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning Jacky,

    have you checked to see what the value of 'stick' is to ensure that the variable is changing.
    that way you know if the collisions are working, then you can focus on why its not showing in the GUI.

    to test your variable, add a debug.log between lines 12 and 13, then the variable will show in the console window for you to check.

    Code (CSharp):
    1. stick += 1;
    2. Debug.Log("stick variable : " + stick);
    3. Destroy(gameObject,0);
    also have a look to see if you have set the reference to your GUIText gameobject. i take it you have dragged into the slot in the inspector if thats the way your referencing it?

    is this script attached to the Stick object?
    just wondering, as if its just needing to check if the player has collided, im not seeing a reason for checking the tag of the stick, if this is the only object where this script will be attached.

    just check if other collider is the player, then do all the stuff. points, destroy etc..

    Obo
     
  3. jacky1234

    jacky1234

    Joined:
    Apr 5, 2015
    Posts:
    2
    Hello, Yes I tried to add the debug.log, the code worked but the counter of the GuiText is not changing....not sure why though..