The type or namespace name ‘player’ could not be found. (Are you missing a directive or an assembly reference)

Ravi

I am making a player script in unity to display it’s health and power-level. Though I am getting the desired output but can someone explain what the above problem is about. here is the code:-

int health = 100;
int power = 0;
string name = Console.Readline("Hey player!! please type in your name. (Kindly do not use your real name)");


public Player()
{
    Debug.Log("health is " + health);
    Debug.Log("power level is " + power);
    Debug.Log("the name of the player is " + name);

}

and the function is here:-

Player Warrior = new Player();

is it really a serious matter that needs to have a look. I have tried calling the function in some other ways too but this only fits my desire. I have tried to find something on google but can’t find an accurate answer

Hardyk Mahendru

What you have written is a constructor:

public Player()
{
    Debug.Log("health is " + health);
    Debug.Log("power level is " + power);
    Debug.Log("the name of the player is " + name);

}

Constructorts are the only methods you can declare that have no return type (because they are always called with the new keyword, and the system returns the new instance for you).

And constructors can only be created in the class that shares the same name as the method itself:

public class Player
   {
   public Player()
      {
      ...
      }
   }

So, either your constructor is outside the class it relates to, or you have forgotten to add a return type: void is not the same thing at all!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related