Can't add script problem (115500)

I don’t understand this. I really don’t. This question has been asked before, I know it has but I’m not finding the answer I’m looking for that would with my predicament. Whenever I try to add a script to an object, I get this, Can’t add component “so so” because it doesn’t exist. Check to see if the file name and class name match. Makes no sense to me. As far as I’m concerned with file names class names, it’s confusing the heck out of me. And I’m stating this now, it’s not a “one script problem” thing. It’s doing the same thing with all the other scripts I have except for the ones that have already been applied to objects, which means I’m not going to post every single code I have. And some of them haven’t been completed or started yet (the reason why is because I just want it already created so I don’t forget later), which makes even less sense, so it would be pointless for me to do that.

So, in a nutshell, I would appreciate it and be grateful if someone can shed some light on what to do here, please. Thank you.

Are you able to create a .unitypackage file and upload it somewhere like dropbox? It will probably help us help you!

–

I had the same issue when declaring delegate types in the namespace scope which used default parameters. Visual Studio was okay with it, but Unity didn't like it.

–

13 Answers

13

Bold words or symbols = Important word or symbol

ok so the code class must match your file name on the 4th line (example: Name of script is Gear so on line 4 it should say public class Gear : MonoBehaviour )

also in coding at end of codes shold have a semicolon(:wink:
Example:

this is not a code just an example;

also the codes must be in a void and the void must be held by the 4th line using { and }

Example:

public class Gear : MonoBehaviour{

void NameWhatEver_NoSpaces{

above the symbol is counting what im saying now as part of code;

}   <-----those ended the two above basically simple organizing.

}

hope this helps :smiley:

And this code is named 'icesword.cs' ?

–

yes it is

–

nope, still didn't work. this is getting too weird

–

You have to fix ALL your errors before you can do anything. The project compiles as a whole not per script. Look at your debug log and fix all the errors.

–

Worked perfectly. I just made a typo in the name of my script. I saved my script as "PlayerControler", but in my script I had public class PlayerController : MonoBehaviour { // blah blah blah } renaming the file to match fixed the issue.

–

You have a bunch of syntax errors. You will not be able to attach scripts that will not compile. You can look what I posted below. I also reformatted it since yours was hard to read.

public class icesword : MonoBehaviour 
{
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.LeftShift)||Input.GetKeyDown(KeyCode.RightShift)); // <-- Don't place a semicolon here
        {
            float distance = 10.0f  // <-- Missing a semicolon
                // attach IceSword to Clark Eagleheart
                // make IceSword appear otherwise don't appear
       ;} //<-- Again with a random semicolon
    }
}

If you place a semicolon at the end of an if statement it just quits right there. Nothing will get called between the braces (true or not true)

regards,

There aren't actually any 'syntax' errors in his code and so it should not stop the code compiling. Ending the if statement early will still compile, just with the wrong behaviour, and the semi-colon is actually in the 'logically' correct place to end the float distance = 10.0f line, as it is not commented out, it is simply the next character and will act the same as if it was written immediately after as you have suggested. Certainly these are mistakes or bad habits but I do not think they would stop the script compiling!

–

I'd appreciate all the help you guys have given but I want to remind you guys that it's not just this code. It's doing the "Can't add script" thing with all the other scripts that haven't been applied yet. So, I really don't think it is a problem within the code but a bug within the program.

–

while you have errors in any script, you can't add anything to objects.

–

elaborate please

–

do you have any errors? if yes, fix them all before you start adding any scripts to the objects.

–

When I had this error it was because the class name and script name didn’t match.
Try using copy/paste to ensure they are EXACTLY the same.

I had the same problem but I fixed it. Here is how you do: Go to the gameobject you wanna add the script to and click on add componet. Add new script, name the Script (If u want) and start coding from their. When you open the Script it will not be in the Script folder so you'll have to drag it their. It works for me so it should work for you. Sorry for my english, I'm from Sweden

–

I get the same error before I’v even edited my scrit so it cant be an error in the code. Have you moved/delited anny of the unity files so that that miht cause an error? I also renamed my script after creating it witch created an error since the scrit name does not atomaticly cange in the code.

Remove the space in the name. it worked for me.

SOLUTION - PROVEN

I just had this same issue come up. And while sometimes it is not an issue, but especially if you have multiple scripts and then you have multiple compiler errors; then you will receive this message.

“Can’t add scripts because the file name cannot be found, check to be sure it matches its class name”

Simply, FIX the problems in all of your compiler errors, or //Comment out the problems you cannot fix. Once this is saved, you will then be allowed to add scripts again.

cheers!

I had this problem caused by naming the file WhateverClass.cs which Unity then tacked another .cs extension onto.

Try to first go to whatever it is you want too add a script to, highlight it go to Add Component scroll too New Skript give it a name than click Create and Add. try copy paste your script there, it worked for me

I had the same problem and I clicked on the gameobject for example “Player” and clicked on add componet and I added new script, named the script and opened the script from their. It worked perfect.

sorry for my english, I’m from Sweden

I found that forgetting to add a Cmd prefix to a [Command] function in another script made all scripts give me this error. Maybe that will help someone.

I was JUST having the same issue. My problem was the script was named Player Controller, but the file was named PlayerController.

The space in between was stopping me from adding the script to the player. Once I made the two match, it added the script.

If you still has this problem try drag-and-drop that particular object that has the script that you need on the place for script. @TheDragonBreather

I have a similar problem. the names are the same but the problem persists. Please help me.
[166340-ekran-alıntısı.png|166340]