1

so I'm new to Unity and I've been trying to test the scene with the script attatched to a character. However, it keeps saying "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." It also says that the name of the file could be different from the name in the code but it isnt, and also it says that the code could be missing MonoBehaviour Scripts. It wont even allow me to attach the script to characters because it cant find the script class.

I've copied and downloaded character movement codes from the internet but they didnt work either. I've also tried deleting and re-making the CS files but that didnt work either. Even adding empty scripts to characters didnt work unless i do it from "Add component"

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { SpriteRenderer sprite; Rigidbody2D rigid; // Start is called before the first frame update void Start() { sprite = GetComponent<SpriteRenderer>(); rigid = GetComponent<Rigidbody2D>(); } private void FixedUpdate() { if (Input.GetKey("d")) rigid.velocity = new Vector2(2, 0); else if (Input.GetKey("a")) rigid.velocity = new Vector2(-2, 0); } } 

There are also these errors in Unity if that helps enter image description here enter image description here enter image description here

2
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jun 5, 2019 at 13:31
  • This bug is now in the official Unity Issue Tracker. issuetracker.unity3d.com/issues/… Commented Jun 7, 2019 at 22:27

4 Answers 4

7

I think your class name is different from file name.

Sign up to request clarification or add additional context in comments.

3 Comments

This did the trick for me. One capitalization difference... It's annoying because it doesn't show up in the compiler, so you have to find it by chance, or go through everything.
How did you find the offending file!?
It wasn't. it was due to a bug which has since been fixed issuetracker.unity3d.com/issues/…
0

Unity apparently can't handle apostrophes (single-quote ') in the directory name of the editor. You need to get rid of the apostrophe in your directory name. Once you make that change, Unity should be able to build the scripts as intended.

Edit: This has been fixed in more recent versions - see https://issuetracker.unity3d.com/issues/scripts-do-not-get-compiled-if-the-unity-editor-path-contains-apostrophes for reference

2 Comments

Are there any other characters that could cause this? I was hopeful on finding this, as the project did have a bunch of files with apostrophes in the path (from a plugin - "NatureManufacture Assets") but removing them prior to importing the project doesn't seem to have fixed the issue
No joy, sadly. Thanks for the suggestion. This is driving me crazy - the same project works fine on my work machine (and all my colleagues' machines) but I can't get Unity to import it on my home laptop (on which a bunch of other work projects work fine).
0

First, it is recommended to use "Add component" to create a script, if you want to attach it to a GameObject, as it automatically imports necessary libraries. Implementing MonoBehaviour is necessary for adding a script to a GameObject.

Second, FixedUpdate() should not be set to private, it does not need an access modifier, just like Start(), see https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.

Third, the errors in your first screenshot seem to imply that there is a problem with your Unity installation. Try reinstalling it and make sure that the Editor you install matches your operating system (64 or 32 bit?).

Fourth, the second screenshot is shown when you use any obsolete libraries or classes, which does not seem to be the case in the script you shared.

Hope that helps.

1 Comment

"Second, FixedUpdate() should not be set to private, it does not need an access modifier, just like Start(), see docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html." This is incorrect. The absence of an access modifier makes it private, which is what one should make it by default (because typically it should not be called explicitly from outside the class) unless one intends to override in which case it should be protected virtual. If someone chooses to make that private nature explicit by adding the keyword then that's purely a matter of style.
-1

It's basically because you deleted some script or renamed it or degraded unity version. you might have to reassign the script at the required position/component.

Note: Make sure that class name is the same as the script name in unity.

1 Comment

They have the same name, and I've tried deleting the script and remaking it. It always goes in the correct folder. If that's what you mean?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.