Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Class component issue that I cannot make sense of, I think

Discussion in 'Scripting' started by justinmed, Mar 30, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    For some reason I cannot get this simple script to plug into my text box. When I try and add it to the text box through the hierarchy panel, it tells me that the class cannot be found and if I create a script through the add component area, nothing shows of it other than it has a script on it, nothing when you click on the drop down arrow. The script should allow a button component to add 1 or subtract one point to the score box. The file is saved as Score.cs.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.  
    9.     public Text score;
    10.     public int scoreAmount;
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         scoreAmount = 0;
    16.         score = GetComponent<Text>();
    17.     }
    18.  
    19.     void Update()
    20.  
    21.     {
    22.         score.text = scoreAmount.ToString();
    23.     }
    24.  
    25.     public void AddScore()
    26.  
    27.     {
    28.         scoreAmount += 1;
    29.     }
    30.  
    31.     public void SubtractScore()
    32.  
    33.     {
    34.         scoreAmount -= 1;
    35.     }
    36. }
    37.  
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,021
    because a text box is not of type Text usually
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Are you sure the Score.cs file is not inside an Editor folder?
     
  4. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    That particular attempt that was Score.cs was stored in a sub script folder in the assets folder that I created. I just tried creating one through the text component and I thought maybe it is because it is TMPro and you cannot just call on text like they did in tutorials from years ago. So I tried this, and I am still getting the same issue. Nothing shows when I drop the arrow down on the script on the text component.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class NewScore : MonoBehaviour
    8. {
    9.  
    10.     public TMP_Text score;
    11.     public int scoreAmount;
    12.    
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         scoreAmount = 0;
    17.         score = GetComponent<TMP_Text>();
    18.     }
    19.  
    20.     void Update()
    21.  
    22.     {
    23.         score.text = scoreAmount.ToString();
    24.     }
    25.  
    26.     public void AddScore()
    27.  
    28.     {
    29.         scoreAmount += 1;
    30.     }
    31.  
    32.     public void SubtractScore()
    33.  
    34.     {
    35.         scoreAmount -= 1;
    36.     }
    37. }
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,209
    If nothing is showing on the component in the inspector, it usually means you have some kind of compilation error preventing Unity from commencing serialisation.

    You need to make sure your class names and script files match exactly, too.
     
  6. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I know, there seems to be no compiling error, no red marks or anything like that. It is odd.
     
  7. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I saw it on a tutorial and it worked for them. But, that was before TMPro, I tried resolving it like that in another reply I made, but that did not seem to do anything.
     
  8. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Can you just post a screenshot of the editor and error?
     
  9. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    Well, the issue is different now. I think my project might be bugged. I pulled up a test project file by creating a new project and then tried the same thing and the scripting added onto the text, but it does not work like it shows in this tutorial. I am just learning trying to do my own thing and am starting with basic achievements and trying to sculpt them into something more intricate, and this was an area of things that I needed to conquer. So, any help is appreciated. Both tutorials use the same method. One would think it should still work.



     
  10. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I got it working! It is because it has to be called out in TMPText like in the updated script I posted and for some reason project is corrupted and I think I just have to import my scenes and needed files into a clean enviornment.