Search Unity

2.6 to 3.1 - 'virtual' keyword no longer works?

Discussion in 'Scripting' started by MattFS, Jan 24, 2011.

  1. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    hey -
    I've attempted to update a project to 3.1 (still have the 2.6 backup) and the first compilation error is this
    Code (csharp):
    1. error CS0106: The modifier `virtual' is not valid for this item
    This code has worked fine for the project life whilst on 2.6.
    Can we not create virtual classes now from a Monobehavior?
    If I remove the virtual keyword it compiles, though the class is made up of many virtual functions that inheriting classes use. So I'm curious what in 3.1 may have caused this error?

    Sorry if this has been dealt with already, I did try searching. Is there perhaps a "What you need to know" about updating a project to 3.1?

    Thanks for any help!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    mono 1.2.5 as used in unity iphone 1.x / unity 2.x was pretty buggy when it comes to especially virtual and where its valid or more appropriately where it is especially not.

    For example, there are no virtual variables in classes nor virtual classes at all (virtual classes would be Abstract classes).
    Also, it should be rather clear that you can not make a class "unreal" thats fully implemented (like the whole MonoBehaviour inheritance tree), you will need to write own abstract classes or use Interfaces which you implement in monobehaviour extending classes for the purpose of defining "protocols" or blueprints

    the only thing that exists in virtual is methods, which you then implement in extending classes through override.
     
  3. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    thanks for the info dreamora - I had considered interfaces for this earlier so I"ll revisit that
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Just to sum up dreamoras spot on reply to this:

    Problem:
    In the earlier version of Mono we used pre-3.0, the virtual keyword was incorrectly allowed in a ton of places. These placements of the keyword didn't even have any effect on execution.

    Fix:
    For each error message, remove virtual keyword.

    The execution of your project should be exactly the same.