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

Question Cannot drag text to game manager

Discussion in 'Scripting' started by Vib3-Ch3cked, Jan 24, 2022.

  1. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    I'm following freeCodeCamp.org's Unity tutorial on the Brick Breaker Tutorial. I've been having issues with applying the score text and lives text to the game manager script so that the script can tell which text boxes on the screen should match the variables. Below is what I wrote for my GameManager's script if it helps. If you need any other information, I'm still a beginner so I might not know what you're referring to automatically.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour
    7.  
    8. {
    9.  
    10.     public int lives;
    11.     public int score;
    12.     public Text livesText;
    13.     public Text scoreText;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.      
    25.     }
    26. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Since nothing is off about that script that I can see, then that means your issue lies with the text objects. Can you screenshot one of them with the inspector brought up on it.
     
    Vib3-Ch3cked likes this.
  3. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    Here is the score text

     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Those are text mesh pro objects you screenshotted.

    Screen Shot 2022-01-24 at 2.43.34 PM.png

    Your code wants a Text object.

    Those will never be the same and will never be interchangeable.

    You probably want to either:

    -
    using TMPro;
    at the top and then use the appropriate tmpro object, or ELSE:

    - start using Text objects in your scene instead of tmpro objects.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Ah, yep, @Kurt-Dekker beat me to it. I figured that was what was going on. I'd suggest sticking with the textmeshpro stuff personally as it does give a nicer look. But, that's completely up to you.
     
  6. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    When I add
    Code (CSharp):
    1. using TMPro;
    , it takes away the option drag my the texts into the Game Manager in the hierarchy. When I click on the GameManager in prefab, the options for lives and score appear, yet I still couldn't drag them into it
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    I think you better review this section of the tutorial... it sounds like you might have missed stuff and I'm not gonna guess which of 50 different things that might be.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  8. Kacidest

    Kacidest

    Joined:
    Jul 12, 2022
    Posts:
    2
    I had the same problem. You're going to want to delete those text objects. Unity interface has changed a bit since the tutorial this code originated from.

    Right click Canvas > UI >
    then DONT choose TEXT - Text Mesh Pro
    Scroll down to the bottom
    Canvas > UI > Legacy > Text

    Follow the rest of the tutorial as normal.
    That should fix the problem.
     
  9. TheAncientGuardian

    TheAncientGuardian

    Joined:
    Feb 25, 2022
    Posts:
    1
    i was having the same problem, you guys save-me. thank you very much hahaha
     
  10. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780

    For TextMeshPro-Objects, you want to use
    TMP_Text
    instead of
    Text
     
    gdoggcasey likes this.
  11. gdoggcasey

    gdoggcasey

    Joined:
    Sep 29, 2021
    Posts:
    2
    Once again I am saved by an old forum post where chatGPT did not realize where I was going wrong. Not following the same tutorial as OP but just wanted to implement a timer to my game and for the life of me could not figure out why I couldn't drag the text game object into the timer public variable. Had no idea TextMeshPro stuff needed a different data type. Must have asked chatGPT 20 different ways and it never mentioned this. Thanks a lot all in this thread!
     
  12. EEddie4

    EEddie4

    Joined:
    May 4, 2023
    Posts:
    2

    Thank you!!!!!