Forum rules - please read before posting.

Using Token on Gameobject

Hi all - I've successfully used the replacement token for a global variable to successfully show a players name on their speaker label (using [var:01] for example). So the replacement token works fine for a player's speaker label as well as for UI items within a menu however I have tried using the tokens on some text that is attached to a gameobject however the text still reads as [var:01] in game instead of changing it to the relevant text inside the variable, any help with why this doesn't work for when I'm using the tokens on a text component that is attached to a gameobject?

Comments

  • Could you provide any more information on how you are using the UI and the variable? I assume the UI attached to the GameObject is somehow connected to a UI in AC's menu manager (ie: Unity UI in scene). I doubt the token will work otherwise. 
  • @Alverik

    Thanks for the quick response in Chris' absence. I believe you've sort of backed up my initial thoughts as to why it wasn't working. The text is simply a text component on a gameobject and is not part of the UI/menu system at all, so I'm guessing that the text component on the gameboject doesn't have any link to AC and therefore no way for AC to read the token and change the text appropriately.

    So, as you've suggested, I'll have to change it so that the text on the gameobject somehow connects to the menu manager to ensure that the token is read and processed as required. This will be easily done, thanks for the speedy response - much appreciated. Great community Chris has built here!
  • Connecting to AC's Menu Manager would indeed do the trick, but it's not strictly the only way to get tokens displaying in your Text component.

    Token conversions are handled with AdvGame's ConvertTokens function, which you can make use of your custom script, e.g.:

    string originalText = "Hi there, [var:1]!";
    string convertedText = AC.AdvGame.ConvertTokens (originalText);

  • @ChrisIceBox


    Firstly welcome back, I hope you had a good break mate - it was well deserved! I did indeed go down the route of linking to the Menu Manager however thanks for the additional advice (despite solving my issue by using the Menu Manager it is still good to know the ConvertTokens function that I can look to use in the future)!

  • edited May 24

    I have a gameObject that has a clock icon and I want it to display the countdown time next to the icon using a Token in the textbox. The problem is that the so token works in menus. I chose to use a GameObject because it will have an initial movement from the middle of the screen to the upper corner triggered by an actionList, for example.

    I used ''3d text'' as a child object in Icone's Object game.

    Any solution?

  • You can still have a GameObject be part of a Menu, if you use Unity UI.

    However, if it's completely detached from the Menu system, you can use a custom script to get the variable token text:

    string newText = AC.AdvGame.ConvertTokens ("[var:1]", AC.Options.GetLanguage ());
    

    What component specifically are you looking to apply this to?

  • edited May 25

    A qual componente especificamente você está procurando aplicar isso?

    The component is ''text mesh''

  • string newText = AC.AdvGame.ConvertTokens ("[var:1]", AC.Options.GetLanguage ());

    I still don't know how to use this code. Would I have to edit the component in the game objectc and insert this code into it?

  • No, you'd attach it to a script on the same object.

    Something like:

    using UnityEngine;
    using AC;
    
    public class TextMeshToken : MonoBehaviour
    {
    
        public string labelText = "[var:1]";
    
        void Update ()
        {
            string newText = AC.AdvGame.ConvertTokens (labelText, AC.Options.GetLanguage ());
            GetComponent<TextMesh> ().text = newText;
        }
    
    }
    
  • It worked, thank you. However, if I want to use another text component like ''TextMeshPro-Text(UI)' the escript gives an error and I try to change the escript for that.

    using UnityEngine;
    using AC;

    public class TextMeshToken : MonoBehaviour
    {

    public string labelText = "[var:1]";
    
    void Update()
    {
        string newText = AC.AdvGame.ConvertTokens(labelText, AC.Options.GetLanguage());
        GetComponent<TextMeshProText(UI)> ().text = newText;
    }
    

    }

    and the following error message appears

    Assets\TripliceReal2.0\ESCRIPTS\TextMeshToken.cs(12,44): error CS1525: Invalid expression term ')'

    I removed the ''(UI)'' but other errors keep popping up.

  • Change it to TMPro.TMP_Text instead.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.