0

I have seen similar questions, but I legitimately can't understand my problem here.

I have created a new script called HealthDisplay, from within Unity itself. It generated with some sample code, and I tried to attach it to a UI object that's text - however that error shows up.

I looked up the error and got all sorts of suggestions, however the code was generated by Unity itself, and the name of the class matched the name of the file exactly. I added some more code and tried to attach it then, but it still didn't work. I also tried reinstalling both Unity and the Unity Hub, and still nothing. Here's the code within HealthDisplay.cs:

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

public class HealthDisplay : MonoBehaviour
{

    private int health = 100;
    public Text healthText;

    // Update is called once per frame
    void Update()
    {
        healthText.text = "HEALTH : " + health;

        if(Input.GetKeyDown(KeyCode.Space))
        {
            health--;
        }
    }
}

The file, I've triple checked, is called HealthDisplay.cs. Its name matches exactly. Is there something I'm doing wrong, or could this just be some weird bug with Unity? Thanks.

ENBYSS
  • 819
  • 1
  • 10
  • 22
  • Works for me. The question should be updated to include desired behavior, a specific problem or error, and the shortest code **and configuration necessary to reproduce the problem.** – Ruzihm Dec 10 '19 at 07:44
  • 1
    I have copied your script, applied it in a test scene and it works fine. Have you checked your console for `Errors`. That might stopping the script to show. – gameDev_Unity Dec 10 '19 at 07:44
  • delete it and try making it again? – BugFinder Dec 10 '19 at 07:51
  • @gameDev_Unity the only things in the console are warnings about invalid .info files within the library. Could it be the version of Unity? I'm using 2019.2.14f1 personal – ENBYSS Dec 10 '19 at 08:23
  • @BugFinder Did that, doesn't work unfortunately. – ENBYSS Dec 10 '19 at 08:23
  • @Ruzihm if it helps, I'm trying to attach it to a Text object under a Canvas. I'm not entirely sure if configurations play into it (and I didn't start the project so I'm unfamiliar with configuration) – ENBYSS Dec 10 '19 at 08:24
  • are you sure there are no other warnings etc? – BugFinder Dec 10 '19 at 08:30
  • @BugFinder yeap, nothing else from what I can see. – ENBYSS Dec 10 '19 at 08:35
  • can you close unity, reopen it with the project and show us the console – BugFinder Dec 10 '19 at 08:59
  • Can you check the spelling on script name? – temre Dec 10 '19 at 10:46
  • Sometimes if you switch between Unity versions or upgrade packages etc it helps to [Clean up your Unity project](https://stackoverflow.com/a/56267992/7111561) and let it recompile the Library and other temporary folders. – derHugo Dec 10 '19 at 11:15
  • @BugFinder nothing shows up - 3 empty errors pop up. just 3 rows with nothing in them. – ENBYSS Dec 10 '19 at 13:46
  • @temre they are both identical - ive used the code generated by Unity, and ive copy pasted the name directly. and nothing – ENBYSS Dec 10 '19 at 13:46
  • @derHugo I could try recompiling the library and etc - though I haven't changed unity versions or upgraded any packages so I'm not sure. – ENBYSS Dec 10 '19 at 13:48

0 Answers0