Search Unity

Question Why is it so hard to make a Score text???

Discussion in 'Scripting' started by LetmeDwight, Dec 10, 2020.

  1. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    I've already watched 20 tutorials but this S*** doesn't work! It can't be that something as simple as a text just doesn't want to work ???? As it is in the whole videos, I wrote the commands and dragged the text objects into the drag & drop column from image 1. But why does the script from picture 2 not write the values in these texts ??? Only what I write directly into the ui text object is displayed, but the crap skilfully ignores what I write into the extern script.
    Picture 1:
    Picture 1.PNG
    Picture 2:
    Picture 2.PNG
    Picture 3 - Just to show that even this is not working: Picture 3.PNG
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Check your console for runtime errors. Add Debug.Log() statements to verify your code is even running. The code as written is fraught with potential failure though. GameObject.Find is designed to return "null" if your GameObject of that exact name is not found. GetComponent is designed to return "null" if a component of that type is not found. You're not testing for either, which would be one of the first debugging steps I'd take.

    Far better would be if you just made a public Text field and dragged the correct Text component to the field instead of trying to find it at runtime. That would entirely eliminate the possibility of those calls returning null, since you wouldn't be making those calls at all.

    But my real suggestion is to entirely separate displaying the score from maintaining the score. Have a score system which updates and maintains the score, with nothing to do with displaying it to the player. Use Debug.Log to QA test your score system. Once that is working, create an entirely separate script which all it does is updates the score Text with the current score, which it gets from the score system through a public method. That way you're not working on two separate problems at the same time, and changes to one don't break the functionality of the other, with them both combined like you're doing. That's my suggestion at least, and how I usually approach these kinds of things.
     
    Vryken, LetmeDwight and Kurt-Dekker like this.
  3. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    So it doesn't work. There is also not a single error message or warning message in the console. I now had to do it the cumbersome Spagethi method by creating a separate script for each text that is added to each exclusive ui text object.
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I don't mean to sound rude when I say this, but ultimately the answer is not that it is hard to make a score text, but that you simply don't know what you are doing. If you accept this and focus on learning why things don't work (rather than concluding that "text just doesn't want to work"), then it will stop being so hard

    ... did you follow Joe-Censored's advice and add debug.log statements to try and figure out why your code isn't working?

    It is very unlikely that the Text component is bugged, and it's more likely that you just don't understand what is going on with your own code. I would suggest trying to work out why it isn't working and fix it. That way, you have a better solution to your problem, and you will also learn a bit in the process.
     
    Last edited: Dec 11, 2020
    Vryken and Joe-Censored like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I feel your frustration but trust me, it can be done.

    It really is a case of knowing where stuff is, and staying patient until you can find stuff reliably and know what connects to what.

    Let's assume this: you have a player with his main
    PlayerController
    script.

    1. ADD 2 variables to this
    PlayerController
    script:
    ---
    public int Score;
    (this is the integer that will store the score)
    ---
    public UnityEngine.UI.Text ScoreText;
    (this is the UI text object)

    2. create a
    Canvas
    and
    Text
    in your scene to be the visible score element

    3. drag the
    Text
    object in the scene onto the
    ScoreText
    reference on your player

    Finally, and this is the only true CODE you need:

    4. write an AddPoints() function in your PlayerController that:
    --- adds the value to the Score integer
    --- updates the ScoreText field

    This would be that function:

    Code (csharp):
    1. public void AddPoints(int points)
    2. {
    3.   Score += points;
    4.   ScoreText = Score.ToString();
    5. }
    That's it. Now in your existing player code, when you want to give points, you just call it!

    Code (csharp):
    1. AddScore(100);
    AddPoints()
    will add to the
    Score
    integer, as well as update the
    ScoreText
    visible field all in one shot.

    If some OTHER part of your game (another script) needs to call that, then three additional things have to happen:

    1. that other place needs a public reference to your player, such as
    public PlayerController MyPlayerReference;


    2. you need to drag the player onto that
    MyPlayerReference
    in the scene

    3. when that other place wants to call AddPoints(), it will need to use the above reference:
    ---
    MyPlayerReference.AddPoints(100);
     
    Last edited: Dec 11, 2020
    seejayjames, Mauri and Joe-Censored like this.