Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

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();



Kindly let me know in case it is a serious problem.

What I have tried:

I have tried calling the function in some other ways too but this only fits my desires.
Posted
Updated 12-May-21 4:01am
Comments
Richard MacCutchan 12-May-21 9:27am    
Where is the Player class declared?

1 solution

What you have written is a constructor:
C#
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:
C#
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900