简体   繁体   中英

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

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

What you have scripted 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM