0
\$\begingroup\$

So I'm just starting to try to learn unity development with C# using this tutorial but I cant add the script to any game object and I get the following error:

enter image description here

Code of the script:

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

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    public Light myLight;

    void Update()
    {
        if (Input.GetKey("space"))
        {
            myLight.enabled = true;
        }
        else
        {
            myLight.enabled = false;
        }
    }
}

I know it's been answered before but I've done everything I read, I did my homework and I still cant get around this issue. I allready checked the name of the class and its the same as the file's name. I think it may be something regarding installation (?) I installed unity hub and it installed Unity and Microsoft Visual Studio by itself.

  • Unity version: 2019.3.3f1
  • MS Visual Studio version: 16.4.5
  • MS Net Framework version: 4.7.03062

I honestly dont know what else to do people! Please help!

\$\endgroup\$
2
  • \$\begingroup\$ If you have no compiler errors, try closing Unity, deleting the "Library" folder in your project, then opening Unity again to force it to re-import everything. This can clear issues with no apparent cause in the source assets. \$\endgroup\$
    – DMGregory
    Mar 6 '20 at 1:07
  • \$\begingroup\$ Why do you use a tutorial that is 5 years old? \$\endgroup\$
    – Vaillancourt
    Mar 6 '20 at 2:43
1
\$\begingroup\$

First of all - try these solutions especially reimporting all. I'm not sure how are you trying to add the script, but it might be helpful:

  1. check if project is saved (serialization might kick in only on save)
  2. select yaml serialization format for your prefabs instead of binary, save, reimport, restart unity and try to add component using Add Component button in inspector of the object (project settings>Editor>Asset Serialization> Force Text)
  3. if this doesn't work try selecting the script in debug mode of the inspector (inspector window has this little padlock icon, next to that is arrow with 3 bars icon, there you select debug. In any component you select the field Script to your script)
  4. Create script from unity with Assets > Create> C# Script menu
  5. make sure that files on disk and asset names in folder have the same case (windows has sometimes some weird problems if someFile is reffered to with different case as SomeFile)
  6. sometimes it's also useful to clear unity log files

And try it on a completely new, blank project.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.