• 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 bembrooks · Oct 01, 2020 at 09:02 AM · scriptable object

Replace String in Scriptable Object,,Replacing text in Scriptable Object

I'm setting up a dialog system for my game, and I'd like to make it so that when there's a string with "!player" in it, it replaces that with a name the player has entered. The way I have it set up is with scriptable objects feeding into a UI canvas. Currently my Scriptable Object looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [CreateAssetMenu(fileName = "New Dialog", menuName = "Dialog Boxes")]
 public class Dialog : ScriptableObject
 {
     public string playerName;
     public string playerPronouns;
     public string talkeeName;
     public string talkeePronouns;
     public string[] dialogEntries;
 
 
     public Sprite[] playerPortraits;
     public Sprite[] talkeePortraits;
 
     private void Awake()
     {
         dialogEntries[0] = dialogEntries[0].Replace("!player", "you");
     }
 }


And my UI canvas script looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
 
 public class DialogDisplay : MonoBehaviour
 {
     public Dialog dialog;
 
     public TextMeshProUGUI playerName;
     public TextMeshProUGUI talkeeName;
     public TextMeshProUGUI playerPronouns;
     public TextMeshProUGUI talkeePronouns;
 
     public Image playerImage;
     public Image talkeeImage;
 
     public TextMeshProUGUI dialogText;
     public int dialogLength;
 
     // Start is called before the first frame update
     void Start()
     {
         playerName.text = dialog.playerName;
         talkeeName.text = dialog.talkeeName;
         playerPronouns.text = dialog.playerPronouns;
         talkeePronouns.text = dialog.talkeePronouns;
 
         playerImage.sprite = dialog.playerPortraits[0];
         talkeeImage.sprite = dialog.talkeePortraits[0];
         dialogText.text = dialog.dialogEntries[0];
         dialogLength = -1;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             dialogLength++;
             {
                 if(dialogLength < dialog.dialogEntries.Length)
                 {
                     dialogText.text = dialog.dialogEntries[dialogLength];
                 }
 
                 else if(dialogLength >= dialog.dialogEntries.Length)
                 {
                     dialogLength = -1;
                     gameObject.SetActive(false);
                 }
             }
             
         }
     }
 }

Right now I'm just trying to have it where it replaces "!player" with "you" in the scriptable object script on the first string, but it won't even do that. It's not returning any errors but it's also simply not doing anything to the string. Does anyone have a good method for doing this?

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

Answer by Bunny83 · Oct 01, 2020 at 09:56 AM

Well, you have several smaller issues here. First of all since your ScriptableObject is an asset, you shouldn't have it modify itself as those changes would persist when you exit play mode in the editor. Second in your code you replace "!player" with "you" only for the first dialog entry. However in your description you said you want to replace it with the name the user has entered. Therefore either your code does not represent your actual code, or your description is wrong. Either way keep in mind that Awake is called right at the game start. So there's no time for the user to enter his name.


I would generally avoid overwriting the original dialog text. It's usually better to replace such markers on the fly when you need the dialog.


Some other minor things that I would call horrible design are:

  • you called your variable "dialogLength" but it seems to indicate the current index and not any length.

  • The fact that this index is always off by 1 leads to some strange condition and reset values. Especially it leaves the value in an invalid state at the beginning (as -1 is not a valid index). It would be much better to adjust the code so the index will actually represent the current index.

So I would recommend to remove the Awake method from your Dialog class. You could add a method like this:

 public string GetDialogText(int aIndex)
 {
     if (aIndex < 0 || aIndex >= dialogEntries.Length)
         return "";
     return dialogEntries[aIndex].Replace("!player", playerName).Replace("!talkee", talkeeName);
 }

This allows your DialogDisplay class to simply request a "fixed" dialog entry where the player and talkee names have been replaced. With the aforementioned changes it would look something like this

public class DialogDisplay : MonoBehaviour { public Dialog dialog;

 public TextMeshProUGUI playerName;
 public TextMeshProUGUI talkeeName;
 public TextMeshProUGUI playerPronouns;
 public TextMeshProUGUI talkeePronouns;

 public Image playerImage;
 public Image talkeeImage;

 public TextMeshProUGUI dialogText;
 public int dialogIndex;

 void Start()
 {
     playerName.text = dialog.playerName;
     talkeeName.text = dialog.talkeeName;
     playerPronouns.text = dialog.playerPronouns;
     talkeePronouns.text = dialog.talkeePronouns;

     playerImage.sprite = dialog.playerPortraits[0];
     talkeeImage.sprite = dialog.talkeePortraits[0];
     dialogIndex = 0;
     dialogText.text = dialog.GetDialogText(dialogIndex);
 }
 
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.E))
     {
         dialogIndex++;
         if(dialogIndex < dialog.dialogEntries.Length)
         {
             dialogText.text = dialog.GetDialogText(dialogIndex);
         }
         else
         {
             dialogLength = 0;
             dialogText.text = dialog.GetDialogText(dialogIndex);
             gameObject.SetActive(false);
         }
     }
 }

}

Comment
Add comment · Show 1 · 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 bembrooks · Oct 01, 2020 at 02:56 PM 0
Share

Thanks so much for all the helpful suggestions! It worked beautifully! I'm pretty new to C# and Unity as you can probably tell. I'd like to understand exactly what's happening with the GetDialogText method because I want to actually learn instead of just copying your solution. is there a documentation article or something explaining it?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 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

Related Questions

Scriptable object gets deleted with a class that holds it? 1 Answer

Error from declaring List within ScriptableObject 1 Answer

Adding behaviors to a game object at run time via scriptable object 1 Answer

Cannot see serialized object's fields in Inspector 1 Answer

Scriptable Object in Editor vs in Build 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges