Static script claims to be Abstract, but isnt [Fixed & Contains Fix!]

Even with the main content commented out, its still claiming to be Abstract, it isnt.

Have I missed something?

using UnityEngine;
using System.Collections;

public static class LockCursor {

    //Maybe replace update with this???
    //'OnMouseDown'
    //

    /*public static void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            Screen.lockCursor = true;
            Debug.Log("LockCursor : cursor is locked");
        }
        if (Input.GetKeyDown ("escape"))
        {
            Screen.lockCursor = false;
        }
    }

    public static void freeMouse(){
        Screen.lockCursor = false;
        Debug.Log("LockCursor : cursor is unlocked");
    }

    public static void captureMouse(){
        Screen.lockCursor = true;
        Debug.Log("LockCursor : cursor is locked");
    }*/
}

It’s a static class is all.

Where is it claiming it’s abstract?

Are you attempting to attach this as a component? Because you can’t do that. (I ask because you have the method Update, and a mention of OnMouseDown, which are messages that can only be received by components).

No where, I just get the warning ā€˜The class named ā€˜LockCursor’ is abstract. The script class can’t be abstract!’

No its not going to be attached to a componant, however the functions ā€˜freeMouse’ & ā€˜captureMouse’ will be executed from a componant on a gameobject (I doubt this is the issue as it works fine on a simmiler script and the triggers havnt been writen yet)

No where, I just get the warning 'The class named ā€˜CursorLock’ is abstract.

Is the above method name a typo, because the method you showed is named ā€œLockCursorā€?

Jeff

1 Like

Yeah, calling the methods from another component isn’t going to cause it.

I see no reason why it’d be doing that.

I just added a static class to a test project with the same code in it, and I don’t get the warning.

Ah sorry, ignore that, I switched the 2 words around incase if it was talking about another script i had and they were colliding or something, no types, the filename & method/script name is ā€˜CursorLock’.

I will quickly update the post, sorry for the confusion

I had a bad feeling this would happen, found a post eariler in the day simmiler to this and the guys did the same thing you did. So maybe a bug (most likely) although i do have the latest version (4.6.1f1).

Any ideas of what I should do? In the meantime i’ll delete the script and recreate it, see if that gives it a kick…

Edit: Scatch that last…

Oh dear, now ive made it worse, the errors gone away and ive now turned it into an instance etc to double check and its now working and im now greeted with the ā€˜The referenced script on this Behaviour is missing!’ Rawr!!!

Fixed!
Cheers

For those finding this thread in the future, why not close it out by describing how you fixed it. There’s nothing more aggravating than searching forever for a solution to an obscure problem, finally finding someone who had the same problem, and the thread ending with ā€œnever mind - I fixed itā€¦ā€

Jeff

Fair point…

The Fix ladies and gents!
ā€˜The class named ā€˜LockCursor’ is abstract. The script class can’t be abstract!’ Fix.
Note: This MIGHT work for all languages (C#, JS & Boo).

Warning: Before you attempt the fix, make a note of where you script is being used. (Right click on the script and select ā€˜Find References in Scene’ and the Hierarchy will state where the script is being used) and then remove the script from ALL gameobjects & prefabs. This will help to avoid the ā€˜The referenced script on this Behaviour is missing!’ warning.

  1. Copy ALL (ā€˜using’ and ā€˜class’ included) the code and paste it to notepad or something.
  2. Delete the script from the Project window
  3. Recreate the script with the EXACT same name
  4. Open it up in MonoDevelop (or what ever program your using)
  5. Paste back in the original code (All of it, ā€˜using’ and ā€˜class’ included)

Now it hopefully should be fixed

Im guessing the reason this happened is maybe the file is corrupted or something went wrong with the file during creation or saving.

This did not work for me unfortunately. My base class inherits from Monobehaviour and is abstract. My class inherits from my abstract base class, but it itself is not abstract. I implement all abstract methods from the base class, but still get the Can’t add script - Can’t add script behaviour. The script can’t be abstract! The error is incorrect, I have plenty of other classes implemented the exact same way, but for some reason this one won’t work.

As a workaround I had to:

  • Comment out the body of the class in question.
  • Set your derived class to inherit from Monobehaviour instead of your absract base class.
  • Add a component of your class to your GameObject.
  • Uncomment the body of your derived class, and inherit from your abstract base class.

Not ideal, but at least I am working again. This is a terrible bug. I have to follow this procedure any time I want to add my derived class to a game object.

In case anybody finds this thread I just had the same problem. It occurred after refactoring some code. Everything looked OK, scripts were saving, but I couldn’t add scripts to objects without a warning. The solution was to check the compiler for errors and sure enough, 2 big red compiler errors sitting in the Unity Console. Sorted those out and re-saved the scripts… compiler errors gone and I could set the app to run… after that, no problem carrying on adding scripts to objects.

Basically, if there are compiler errors in Unity it will not compile the scripts you have written even if you have changed them in an editor to be correct.