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 chimera1993 · Apr 18, 2018 at 01:51 AM · stringpublicprivatescopereturn value

cannot return private string variable through a public get function?

public class MapTile : MonoBehaviour { private string mapName;

 public void SetMapName(string t_mapName){
 mapName = t_mapName;
 }

 public string GetMapName(){
     return mapName;

} }

Basically, i have a class like this. I had the string set somewhere else, and expected to be able to get it through the get function, but i couldn't.

However, when i changed the string from private to public it turned out working. Why is that? I just want to keep this string variable private. Please help!

Comment
Add comment · Show 3
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 $$anonymous$$ · Apr 18, 2018 at 01:55 AM 1
Share

What errors are you having?

avatar image JedBeryll · Apr 19, 2018 at 04:10 AM 1
Share

I don't see a problem in this code, maybe it's somewhere else.

avatar image Bunny83 · Apr 19, 2018 at 04:41 AM 0
Share

cannot return private string variable through a public get function

What exactly doesn't work here? Do you get a compile error? A runtime error? No error but an empty or null string? Does the game crash? Does your cat walk backwards up the wall?


Since this question is a general program$$anonymous$$g question and not really related to Unity i'll move it into the HelpRoom. Please improve your question by editing your question and adding more details.

1 Reply

· Add your reply
  • Sort: 
avatar image
-2

Answer by melsy · Apr 18, 2018 at 02:15 AM

You already have mapName so why are you running another method to get it. That is kind of redundant.

 string mapName = "";

 public void SetMapName(string _name)
 { 
     mapName = _name;
 }

 Debug.Log(mapName);
Comment
Add comment · Show 4 · 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 sean244 · Apr 18, 2018 at 05:19 AM 0
Share

Because he wants to keep it private and still access it from other scripts.

avatar image melsy · Apr 19, 2018 at 03:53 AM 0
Share

Why down vote.. This accomplishes the goal and keeps the mapName private.

avatar image sean244 melsy · Apr 19, 2018 at 04:01 AM 0
Share

That's the exact same code that he posted in his question, except you didn't address why the get function isn't working.

avatar image Bunny83 · Apr 19, 2018 at 04:37 AM 1
Share

I did not downvote and sean didn't downvote as well, but i think i understand why. First and foremost having the actual fields private and providing accessor methods is a fundamental part of OOP (Encapsulation). Though even we usually use properties in C# for that purpose it's totally fine to use normal methods. Properties are just syntactic sugar for such get and set methods.


The second issue with your answer is that you keep the mapName variable private so the second class that was mentioned can't access it and since you removed the accessor method it can't be accessed at all outside the class.

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

80 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

Related Questions

How to change a public location,how to get a public variable 1 Answer

Can I have a variable accessed through the inspector and be accessed by a child script without making it public? 1 Answer

NullReferenceException: Object reference not set to an instance of an object & You are trying to create a MonoBehaviour using the 'new' keyword. 0 Answers

public or private, but what about no class before a variable? 3 Answers

Generate ladder on y axis 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