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 /
avatar image
0
Question by Ochreous · Oct 17, 2015 at 06:01 AM · c#inspectorstringmanualassignment

C# String Requires Manual Assignment in Inspector

My String variable stringText keeps requiring that I manually assign it in the Inspector. If I don't then it's left blank and my save function saves a blank string. It should be automatically assigning to a value like with Floats and Strings. Any idea how I could get around this?

 using UnityEngine;
 using UnityEngine.Events;
 
 public class UserSettingString : MonoBehaviour
 {
     public string stringText { get; set; }
 
     public UserSettingEventString onSettingLoaded;
 
     void OnEnable()
     {
         Load();
     }
 
     public void Load()
     {
         onSettingLoaded.Invoke(stringText = PlayerPrefs.GetString(name));
     }
 
     public void Save()
     {
         PlayerPrefs.SetString(name, stringText);
     }
 }
 
 [System.Serializable]
 public class UserSettingEventString : UnityEvent<string>
 {
 }
 




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
0

Answer by NewPath · Oct 17, 2015 at 08:31 PM

You'll need to make it a field, not a property for it to work correctly in the inspector. The inspector cannot properly serialize properties.

Comment
Add comment · Show 5 · 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 Ochreous · Oct 18, 2015 at 12:29 AM 0
Share

What exactly do you mean? I already have a field for both my UserSettingEventString Class and my string stringText in my main UserSettingString class.

  public class UserSettingString : $$anonymous$$onoBehaviour
 {
 public string stringText { get; set; }
 public UserSettingEventString onSettingLoaded;





avatar image Bunny83 Ochreous · Oct 18, 2015 at 01:45 AM 0
Share

No, you don't have a field, you have declared an auto property called stringText. onSettingLoaded is an actual field / variable but stringText is not.

Declare it like this:

 public string stringText;

edit
In case you don't know what a property is read this and this.

avatar image Ochreous Bunny83 · Oct 18, 2015 at 03:11 AM 0
Share

Ok I've made stringText into a field. But now I can't select stringText in a UnityEvent field within the inspector. How do you serialize stringText without turning it into a property? You can't declare a variable outside of a class and you can't put [System.Serializable] inside of a class.

Edit: I did find [SerializeField] which allowed me to serialize stringText. However I still can't select stringText in a UnityEvent field within the inspector.

Show more comments
avatar image Ochreous · Oct 18, 2015 at 05:18 AM 0
Share

Basically the UserSettingString's Script runs through several different GUI gameobjects. I have a total of 4 gameobjects the string test gameobject, change text button, save button and load buttons. The Change Text button changes the text to String Text. The String test gameobject contains the userSettingString Script and is the gameobject that all of the GUIs reference. Save and Load execute their respective functions. I can compress the assets and put them into a unitypackage if necessary. I realize this is a bit difficult since you can't actually see my project.

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

32 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to make a single word in a Text object clickable / have hover. 2 Answers

C# Variable 4 Answers

Can i give GetComponent a variabel instead of a scriptname? 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