Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TheAngryKiwi · Nov 14, 2016 at 04:08 PM · c#variablesclassproperties

Can someone explain this to me? Player myPlayer = new Player();

Player myPlayer = new Player();

So, there is another script (and class) named Player, which I think the script is trying to access, but I don't understand this line of code. I'm starting to learn Intermediate Gameplay Scripting, and I got confused with this. Please help!

Here's the video I'm learning from: https://www.youtube.com/watch?v=BYwCXJ-J9dA (it's an Unity Official Tutorial)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheAngryKiwi · Nov 14, 2016 at 04:12 PM 0
Share

Also, I think myPlayer is the name given to the variable we're creating, but I don't understand "= new Player()"

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Landern · Nov 14, 2016 at 06:35 PM

Classes in c# are an implementation(also called a type) that can be instantiated(created in memory) to represent something.

When

 Player myPlayer = new Player();

is invoked in code, it is creating a new object called "myPlayer" which all the fields/properties/methods that exist. Right before the semi-colon at the end of the line is the parameters to pass to the constructor of the class when being created. By default there is a parameterless constructor, if no logic needs to take place you don't need to define it in the class, the below is the same as c# will create a default when building out the code. You will also notice that constucters don't have return types or marked as void(they return nothing).

 public class Player
 {
   private string m_name; // field, a backing field for the property called Name
 
   public Player()
   {
     // empty constructor
   }
 
   public string Name
   {
     get { return m_name; } // returns the private m_name field .
     set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
 
   }
 }


 // this class while doesn't explictly have a constructor does still have a constructor.
 public class Player
 {
   private string m_name; // field, a backing field for the property called Name
 
   public string Name
   {
     get { return m_name; } // returns the private m_name field .
     set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
 
   }
 }

constructors can call constructors before so you can combine logic

 public class Player
 {
   private string m_name; // field, a backing field for the property called Name
   private int m_player_id; // field for a player id
 
   public Player()
   {
     m_player_id = 1928;
   }
 
   // if this constructor was used it would allow you to pass in a player name and still call the default parameterless constructor which sets the m_player_id to 1928.
   public Player(string playerName)
     : this()
   {
     m_name = playerName;
   }
 
   public string Name
   {
     get { return m_name; } // returns the private m_name field .
     set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
 
   }
 }

So at the end of the day, what you're asking about is just the creation of an object that either has login in it, or doesn't, but once instantiated the other members of the class(field/properties/etc) are available for you to manipulate.

You should have a class somewhere called Player which in the tutorial they do create.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheAngryKiwi · Nov 15, 2016 at 12:22 AM 1
Share

Thank you very much!

avatar image ThePersister · Nov 15, 2016 at 09:53 AM 0
Share

Well explained! Didn't know you could combine constructors within the same class, aside from inheritance tricks, awesome! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

259 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using variables from another script. 0 Answers

Initialize Variable vs override in public class 1 Answer

Not sure how hard this is: Unity Networking class system. 1 Answer

check if a list contains the same id as in another list 1 Answer

Declared variable being returned as null in a method? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges