Cannot write my code right, because of an error CS0111 - c#

I'm a beginner in programming, So i don't know how to fix this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieScript : MonoBehaviour
{
public Transform player;
public Transform zombie;
public GameObject zombieScript;
bool canActive = false;
void Start()
{
if (canActive == false) { zombieScript.SetActive(true); }
}
void Update()
{
UnityEngine.Debug.Log(Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)));
if (Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)) <= 10.00000 && canActive == false)
{
zombieScript.SetActive(true);
canActive = true;
}
}
}
Unity writes:
Assets\ZombieScript.cs(5,14): error CS0101: The namespace '' already contains a definition for 'ZombieScript'
Assets\ZombieScript.cs(12,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Start' with the same parameter types
Assets\ZombieScript.cs(17,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Update' with the same parameter types
in advance, thank u <3

A basic structure of a C# class looks like this:
using ...
namespace CompanyXYZ.ProductABC
{
public class Entity
{
// Constructors, properties, methods & etc.
...
}
}
CS1011 compiler error will be thrown whenever it found more than one definition within the same namespace.
Namespace allows us to avoid conflict of classes with the same name by grouping them in different namespaces.
In this case, you probably have more than one ZombieScript defined under the same namespace which is not shown in your code. Therefore, kindly check if you have it defined somewhere else (within the same namespace) and if found it you're good to go!

Related

Unity How to declare propertly Input Actions in Script CS0246

I was following a tutorial for scripting Input Actions in Unity
After finished coding what the tutorial told I've got this error in the declarations of Input System's stuff CS0246 The type or namespace name 'InputActions' could not be found
The code that causes the error is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GameInput : MonoBehaviour
{
private InputActions InputActions;
private InputAction StartAction;
void Start() {
InputActions = new InputActions();
}
private void OnEnable() {
StartAction = InputActions.Start.Start;
StartAction.Enable();
InputActions.Start.Start.performed += StartGame;
InputActions.Start.Start.Enable();
}
public void StartGame(InputValue value) {
// CODE HERE
}
}
The error is caused by the declaration private InputActions InputActions;
I think it is by the variable type InputActions that may not exist.
But the tutorial also uses a type that doesen't exist.
How can I make Unity to recognize the InputActions type?
Typo: Unity does not have an InputActions Type, but it has a InputAction Type. See how you spelled the type different in your example. InputActions does not exist so you can't use it.
Change InputActions to InputAction.

Unity namespace error does not exist with SceneManager

I am using unity SceneManager but it gives me a namespace error that it does not exist in
using UnityEngine;
using UnityEngine.SceneManagement;
public class ManageGame : MonoBehaviour
{
bool gameHasEnded = false;
public void GameOver()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Restart();
}
}
void Restart ()
{
SceneManagement.LoadSceneMode(SceneManager.GetActiveScene().name);
}
}
I read the official documentation about SceneManger and i checked the using part and it was the same.
My version is 2019.3.14.
Why does this happen?
Could you post some more information about your script? Which method of SceneManager are you trying to call and how exactly does this code look like? What's your Unity version? What are your other 'using' statements?
Without having more details we can only guess: Both the 'UnityEditor' as well as the 'UnityEngine' namespace contain a SceneManagement namespace, which might be causing some confusion.
EDIT:
Thanks for updating the question with more information. I see two issues:
1.) You are trying to call a method on a namespace (SceneManagement is a namespace not a class). Instead you want to access the class SceneManager in the SceneManagement namespace.
2.) LoadSceneMode() is not a method in SceneManager. There's an enum with that name, but no method. You want to use the method LoadScene()
So the correct line would be:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Why do I need fully qualified name to access static property of a class that was included with "using"

If I have this class declaration
namespace DatabaseCache
{
public class DatabaseCache
{
public static bool somePublicFlag ;
}
}
In another class I have this statement
using DatabaseCache;
Why do i need to write a statement like this in that class
DatabaseCache.DatabaseCache.somePublicFlag = true ;
instead of just
DatabaseCache.somePublicFlag = true ;
You don't need that if you don't have a Namespace and Type name collision. A well designed library will not have such collision, So design your library accordingly to avoid such collision.
namespace DatabaseCache
{
//change the name of the class
public class DifferentNameThanNamespace
{
public static bool somePublicFlag ;
}
}
Because the compiler doesn't know if DatabaseCache is referring to the namespace or the class. Even thought you're using the namespace it's still perfectly legal to preface types within that namespace by the namespace, so the call is ambiguous.
You could alias the type by using:
using DC = DatabaseCache.DatabaseCache;
and then just calling
DC.somePublicFlag
but that's just masking the problem - renaming the namespace is a better solution.
In order to avoid ambiguities, it is not recommanded to declare a class with the same same as its namespace.
The Framework Design Guidelines say in section 3.4 “do not use the same name for a namespace.
To learn about how to activate/disable this kind of warning see this link
(Of course the advice is to not use the same name for two distinct things.)
Answer with an extension. Suppose you have one file with:
namespace DatabaseCache
{
public class DatabaseCache // same name as namespace :-(
{
public static bool somePublicFlag;
}
public class somePublicFlag // evil other type
{
}
}
Then now it depends on where you put your using directives relative to your namespace. For example, in another file, this will be legal:
namespace Other
{
using DatabaseCache;
class DbcTestClass1
{
void M()
{
DatabaseCache.somePublicFlag = true; // legal!
}
}
}
In the above example, somePublicFlag refers to the field of the class!
However, this is legal as well:
using DatabaseCache;
namespace Other
{
class DbcTestClass2
{
void M()
{
var instance = new DatabaseCache.somePublicFlag(); // legal!
}
}
}
With that placing of the using directive, the somePublicFlag refers to the "evil" class of that name. The qualifier DatabaseCache. in this case is redundant, but it is still seen as a reference to the namespace global::DatabaseCache because the global namespace (null namespace) is searched first in this case.
To learn more, see my answer elsewhere. It all depends on the order in which the different namespaces (including the global namespace) are searched for a matching name.

Why does the C# compiler differentiates between these 2 cases?

When you have some property that's like:
using Algebra;
public Algebra.Vector3 Direction
{
get { return this.direction; }
}
then compile and later change it to:
using Algebra;
public Vector3 Direction
{
get { return this.direction; }
}
it seems like the compiled code is different between the two assemblies, which I could see using the Reflector.
Why does the compiler differentiates between the two code? Isn't it only necessary to see if there is any ambiguous type at compile time and if there isn't, have the compiled code be the same for both? I would assume the compiled code to use fully qualified names for every member at all times.
I can't reproduce this. Sample code:
namespace Algebra
{
public class Vector3 {}
}
namespace Test
{
using Algebra;
public class Program
{
private Vector3 direction = null;
public Vector3 Direction1
{
get { return direction; }
}
public Algebra.Vector3 Direction2
{
get { return direction; }
}
}
}
The generated IL for the two properties is exactly the same, and they look the same in reflector.
My guess is that you've actually got another class called Vector3 in the "global" namespace (or some other namespace that you have a using directive for).
Try hovering over both type names in Visual Studio and see what they show.
Here is an example that would generate the results you are observing. Hopefully this clears up what you’re asking. Assuming you have
a separate library containing the type Vector3 in a namespace Algebra;
the following files in your project:
File ①
namespace NotAlgebra
{
public class Vector3
{
// ...
}
}
File ②
using Algebra;
namespace NotAlgebra
{
public class XYZ
{
// Refers to NotAlgebra.Vector3 (defined in File 1 above)
Vector3 MyProperty1 { get; }
// Refers to Algebra.Vector3 (defined in the external library)
Algebra.Vector3 MyProperty2 { get; }
}
}

Namespace collisions

How is it possible that .NET is finding the wrong 'MyType' in this scenario?
I have a type A.B.C.D.MyType in a project that I'm working on, and I'm referencing a DLL that has a type A.B.MyType? I do not have any 'using A.B;' statements anywhere in my code, and I do have 'using A.B.C.D;'. When I compile, the compiler thinks any naked reference to 'MyType' means 'A.B.MyType'.
I know I could just rename the class or use an alias, but I'm wondering how this is even possible.
Any ideas?
Thanks!
Are you working in a namespace that is under A.B namespace? (for example A.B.X) if so the C# namespace resolutions (ECMA-334 C# Language Specification : 10.8 10.8 Namespace and type names) says:
... for each namespace N, starting
with the namespace in which the
namespace-or-typename occurs,
continuing with each enclosing
namespace (if any), and ending with
the global namespace, the following
steps are evaluated until an entity is
located...
and then followed by:
If K is zero and the namespace
declaration contains an
extern-alias-directive or
using-aliasdirective that associates
the name I with an imported namespace
or type, then the
namespace-or-type-name refers to that
namespace or type
This means that name resolution starts at the current namespace and searches all namespaces up to the root, and only after this hierarchical search ends, then the namespaces imported with the using clause are searched.
The following example prints "Ns1.Foo"
using Ns1.Foo.Foo2;
namespace Ns1.Foo
{
class Foo
{
public void Print()
{
System.Console.WriteLine("Ns1.Foo");
}
}
}
namespace Ns1.Foo.Foo2
{
class Foo
{
public void Print()
{
System.Console.WriteLine("Ns1.Foo.Foo2");
}
}
}
namespace Ns1.Foo.Bar
{
class Bar
{
public void Print()
{
new Foo().Print();
}
static void Main()
{
new Bar().Print();
}
}
}
Edit: Adding a using clause inside a namespace, will make so that the namespace is searched before the hierarchical search of current namespace is done is done. Change the example to:
namespace Ns1.Foo.Bar
{
using Ns1.Foo.Foo2;
class Bar
{
public void Print()
{
new Foo().Print();
}
static void Main()
{
new Bar().Print();
}
}
}
and Ns1.Foo.Foo2 will be printed.
Edit: changed example
Is your code in namespace A.B or A.B.C? If so, that's probably the issue. Use a using directive like this:
using TheTypeIWant = A.B.C.D.MyType;
then just refer to TheTypeIWant in your code.
EDIT: I've just tried the "using MyType=A.B.C.D.MyType" option, but that doesn't work. The above is fine though.
Just a guess: in your project properties, is the "default namespace" set to A.B ?

Categories