Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SwathedOrange · Apr 20 at 02:25 PM · inputstringscriptableobjectinputfield

How to get an input and put it into a scriptable object?

I'm trying to make a game where players input their names and they get random questions. For an example if the player inputs a name "John", the question will be "John have you ever done bla bla". So I'm wondering if there's a way to get the input from 1 script, and then in ScritableObject script, assign that input to a string or when i create the asset of that ScriptableObject be able to decide at what part of the string does the name from an input appear. My question might be confusing, so basically is there a way to use string interpolation in scriptable objects?

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
Best Answer

Answer by Hellium · Apr 20 at 02:42 PM

Why not simply use string formatting?

 [SerializeField]
 private string[] questions = new string[]
 {
     "Hello {0}!",
     "{0} have you ever done bla bla"
 };
 
 
 // To be set from the input's value
 public string Interlocutor { get; set; }
 
 public string GetQuestion(int index)
 {
      return string.Format(questions[index], Interlocutor);
 }


You may have questions with other inputs, so you'll need a more custom solution using a simple Replace for instance.

 [SerializeField]
 private string[] questions = new string[]
 {
     "Hello {{interlocutor}}!",
     "{{interlocutor}} have you ever {{action}}",
     "You have {{coins}} coins",
 };    
 
 // To be set from the input's value
 public string Interlocutor { get; set; }

 public string Action { get; set; }

 public int Coins { get; set; }
 
 public string GetQuestion(int index)
 {
     var sb = new StringBuilder(questions[index]);
     sb.Replace("{{interlocutor}}", Interlocutor);
     sb.Replace("{{action}}", Action);
     sb.Replace("{{coins}}", Coins);
     return sb.ToString();
 }
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 SwathedOrange · Apr 20 at 02:47 PM 0
Share

I thought about this and im literally making this right now, but the problem is, what if later i have lets say 200 questions, is it a good idea to write it all like this? The code would look too big and ugly.

avatar image Hellium SwathedOrange · Apr 20 at 02:53 PM 1
Share

Your questions will be set in the inspector. Here, I did it in code for the sake of the example.

You could also have a Dictionary to set the input instead of fields.

  private Dictionary<string, string> parameters = new Dictionary<string, string>();
  
  public void SetParameter(string key, string value)
  {
      parameters[key] = value;
  }
  
  public string GetQuestion(int index)
  {
      var sb = new StringBuilder(questions[index]);
      foreach(var parameter in parameters)
          sb.Replace(parameter.Key, parameter.Value);
      return sb.ToString();
  }
avatar image SwathedOrange Hellium · Apr 20 at 03:11 PM 0
Share

You are right, thank you, btw when writing strings in inspector because of [SerializeField], if I write lets say:

{{playerName}} take 3 sips

it will drag the playerName variable out of the script and attach it to the string?

Show more comments

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

171 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

Related Questions

Inputfield set as Variable 1 Answer

Unity mobile keyboard like "WhatsApp" 0 Answers

Half of InputField unresponsive 1 Answer

Getting the text from UIInput 1 Answer

How to schedule responses (based on player's input text) for text adventure game? 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