33_Munir_33
33_Munir_33

Reputation: 23

Unity "The associated script can not be loaded" and "Win32Exception: The system cannot find the file specified"

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

Upvotes: 1

Views: 29602

Answers (4)

Ruzihm
Ruzihm

Reputation: 20269

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

Upvotes: 0

Marshall
Marshall

Reputation: 79

I think your class name is different from file name.

Upvotes: 7

Ahorn
Ahorn

Reputation: 649

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.

Upvotes: 0

Shreyansh Anchlia
Shreyansh Anchlia

Reputation: 26

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.

Upvotes: -1

Related Questions