Error: Unity Warning CS0108
What's Causing This Error
The 'CS0108' is a Compiler Warning typically seen in Unity. It surfaces when a member in a class or struct is declared with the same name as another member inherited from a base class. In other words, you have a derived class that hides an inherited member from the base class without using the new
keyword, causing ambiguity. The warning is simply letting you know that there could be some confusion later if you're expecting to access the base member but are actually accessing the derived member.
For instance, if a base class 'A' has a function named 'foo()', and you have a derived class 'B' which also has a function named 'foo()', this warning will pop up because 'foo()' in 'B' is hiding 'foo()' in 'A'.
Solution - Here's How To Resolve It
There are a couple of strategies that you can use to resolve this warning:
-
Use the
new
keyword: This keyword is used to hide an inherited member and avoid any potential ambiguity. By doing so, you're explicitly telling the C# compiler that you intend to hide the base class method. If we take the example mentioned above, in class 'B' you would define your method like this:new void foo() { ... }
. -
Rename the member: Another straightforward approach is to rename one of the members. This solution is especially useful when you don't actually need to override the base class method or when the derived class method performs a completely different operation than the base method.
-
Implement overriding properly: If your intention was to override the functionality of the base class method in the derived class, use the
override
keyword instead of just naming a method with the same name. The base class method must be marked asvirtual
,abstract
, oroverride
.
It's important to choose the right solution based on your use case and programming intention. Sometimes, hiding a base class member is necessary, but often renaming or properly overriding the method can lead to cleaner, less confusing code.
Was this content helpful?
Other Common Game Engines Errors (with Solutions)
- godot unindent does not match
- godot error calling method from signal
- godot unable to load .net runtime
- godot unable to write to file
- godot error constructing a gdscript instance
- godot script does not inherit from node
- godot unable to initialize video driver
- godot is_on_wall not working
- godot button not working
- godot error loading extension
- godot warning treated as error
- godot could not create child process
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost