Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question ERROR the associated script cannot be loaded

Discussion in 'Editor & General Support' started by JustJP2K, Apr 7, 2023.

  1. JustJP2K

    JustJP2K

    Joined:
    Aug 24, 2020
    Posts:
    5
    Hello, im new to unity and was trying to add a script to my game object when all it showed was this error: The associated script cannot be loaded, please fix any compile errors and assign a valid script. Every single time. to every new project.

    Dont know what to do.

    Any help would be much appreciated.

    Im using Unity version 2021.3.22f1
    And Visual Studio Code

    Thank you
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    The steps to fix this problem are:
    • Fix any and all compile errors in your project. You can see compile errors in the console window in Unity.
    • Make sure your script's class derives from MonoBehaviour
    • Make sure your script's class name matches the filename. E.g.
      public class PlayerMovement
      should only appear in a file named "PlayerMovement.cs"
     
  3. JustJP2K

    JustJP2K

    Joined:
    Aug 24, 2020
    Posts:
    5
    Everything is as you say. Let me make a quick demonstration video maybe that will help.
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    The name of the class must match the name of the file. Unity is trying to find a class called "Controller" from that file and getting confused.

    Rename the file and it'll work.
     
    BarriaKarl likes this.
  5. JustJP2K

    JustJP2K

    Joined:
    Aug 24, 2020
    Posts:
    5
    i feel very stupid right now. thank you for the help!
     
  6. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    It's certainly confusing! Before I realized what you had done wrong, I was struggling to remember exactly what weird context I'd seen this in before.