Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by DarkEcho, Dec 28, 2014.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Even with the main content commented out, its still claiming to be Abstract, it isnt.

    Have I missed something?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class LockCursor {
    5.  
    6.     //Maybe replace update with this???
    7.     //'OnMouseDown'
    8.     //
    9.  
    10.     /*public static void Update() {
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             Screen.lockCursor = true;
    14.             Debug.Log("LockCursor : cursor is locked");
    15.         }
    16.         if (Input.GetKeyDown ("escape"))
    17.         {
    18.             Screen.lockCursor = false;
    19.         }
    20.     }
    21.  
    22.     public static void freeMouse(){
    23.         Screen.lockCursor = false;
    24.         Debug.Log("LockCursor : cursor is unlocked");
    25.     }
    26.  
    27.     public static void captureMouse(){
    28.         Screen.lockCursor = true;
    29.         Debug.Log("LockCursor : cursor is locked");
    30.     }*/
    31. }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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).
     
  3. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    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)
     
    Last edited: Dec 28, 2014
  4. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    > 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
     
    lordofduct likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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.
     
  6. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    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
     
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    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...
     
  8. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Edit: Scatch that last...

     
  9. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Fixed!
    Cheers
     
  10. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    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
     
  11. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    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.
     
  12. shloob

    shloob

    Joined:
    Jun 1, 2013
    Posts:
    10
    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.
     
  13. shloob

    shloob

    Joined:
    Jun 1, 2013
    Posts:
    10
    As a workaround I had to:

    1. Comment out the body of the class in question.
    2. Set your derived class to inherit from Monobehaviour instead of your absract base class.
    3. Add a component of your class to your GameObject.
    4. 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.
     
  14. AppDragn

    AppDragn

    Joined:
    May 21, 2017
    Posts:
    7
    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.