Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jmgek · Mar 02, 2015 at 08:19 PM · propertiessettersgetters

Proper use of c# properties?

Hey guys I have a question about Getters/Setters. I need to know if I am doing it correctly. Or is there a better way? Just trying to make my code more professional. Thanks!!!!

 //GameManager
 public void CreateChar()
 {
     GameObject tempChar = GameObject.CreatePrimitive(PrimitiveType.Cube);
       
     CharManager tempComponent = new CharManager();
     tempComponent = tempChar.AddComponent<CharManager>();
 
     tempComponent.NameOfCharacter = "Random Name";
 }
 
 //CharManager that is referenced
 //Header

 private string nameOfCharacter;
 public string NameOfCharacter
 {
     get
     {
         return nameOfCharacter;
     }
     set
     {
         nameOfCharacter = value;
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by _foa · Mar 02, 2015 at 08:25 PM

It looks ok to me, maybe you can use an autoproperty (see: https://msdn.microsoft.com/en-us/library/bb384054.aspx)
For a more detailed discussion with many examples about properties you can see: https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

I hope it helps :)

Comment
Add comment · Show 3 · 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 jmgek · Mar 02, 2015 at 08:28 PM 0
Share

This is great! I thought there was an other way as opposed to assigning Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager();

Thanks for the quick reply!!!

avatar image jmgek · Mar 02, 2015 at 08:47 PM 0
Share

I just have one question, On my char$$anonymous$$anager how would I be able to initilize it so it is not new all the time? Like this?

   //$$anonymous$$ain.cs
  public void CreateChar()
     {
         GameObject tempChar = GameObject.CreatePrimitive(PrimitiveType.Cube);
       
         Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager("This", 1);
 
         Char$$anonymous$$anager.Instance.CheckName();
     }
 
 //Char$$anonymous$$anager.cs

public string NameOfCharacter { get; set; } public int HealthOfCharacter { get; set; }

 public Char$$anonymous$$anager(string name, int health)
 {
     NameOfCharacter = name;
     HealthOfCharacter = health;
 }

   public void CheckName()
     {
         Char$$anonymous$$anager character = this;
 
         //character.HealthOfCharacter = Random.Range(10, 100);
         Debug.Log(character.NameOfCharacter);
     }
avatar image _foa · Mar 03, 2015 at 09:28 AM 0
Share

Just a small thing on your first question code, I think you can do:

 Char$$anonymous$$anager tempComponent = tempChar.AddComponent<Char$$anonymous$$anager>();

In$$anonymous$$d of:

  Char$$anonymous$$anager tempComponent = new Char$$anonymous$$anager();
  tempComponent = tempChar.AddComponent<Char$$anonymous$$anager>();

Anyway, if you need a single instance class (a class that is initialized only once and shared across different objects) you can take a look at the Singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern and for a C# implementation of the pattern see: http://csharpindepth.com/Articles/General/Singleton.aspx

From the code posted on your comment it is not quite clear to me what your intentions are, as I can't see how Char$$anonymous$$anager.Instance is declared. Also, in the CheckName() method, it is redundant assigning this to a temporary variable to access the class members, you can use the member name directly it is more clear to read.

If you do not want an object be different all the time instantiating it all the time with new is not the correct way to avoid that as new instantiates a new object every time :)

Hope I've been clear, let me know :)

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

21 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

Related Questions

Accessing class properties through getters & setters in editor mode 1 Answer

Properties names, same as class okay or bad? 1 Answer

Get and Set difference 2 Answers

problems with setters and getters. 1 Answer

getter and setter not working 1 Answer


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