Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Referencing Text Object (newbie question)

Discussion in 'Scripting' started by ey97, Aug 3, 2018.

  1. ey97

    ey97

    Joined:
    Jul 6, 2018
    Posts:
    3
    Not been coding very long so be gentle pls

    I'm looking for a way to reference when the text component of a game object is being updated so I can play a sound from FMOD (it isn't an Fmod issue, I know how to use that Im just not amazing at writing code)

    my logic is something along the lines of: when text is updating > text update = true > play sound

    I just need it to be true when updating and false when it's not

    sorry if thats a stupid way of explaining it

    Thanks in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    There's no way (that I know of) to know from a stock UnityEngine.UI.Text item that it is being updated.

    Generally the way this is approached is somewhere in the game you know something happened and that this something might require some output, perhaps a text updating and a noise happening, so you plumb both of those things in.

    That way if in the future you decide to remove the Text, or replace it with something else like a TextMeshPRO, the sound will continue working.

    I have a system I call 'datasacks' that lets you do handy event subscription like this. See the repo at:

    http://bitbucket.org/kurtdekker/datasacks

    It has a diagram but basically what you would want to do is have a datasack called whatever it is that is the quantity you are displaying, and hook that up with a DSTextDisplay-style script to your Text field.

    Second you would have a script that also subscribes to changes on that variable and does the sound triggering you want. IN FACT, there already is a datasack script called DSAudioPlay.cs that would play the sound for you anytime the datasack changes, so that might be close to what you're looking for.

    There's a simple example game in the above repository.
     
  3. ey97

    ey97

    Joined:
    Jul 6, 2018
    Posts:
    3
    Should have been a little clearer on the description of exactly what I'm trying to do, my apologies (it's late haha)

    The sound that's playing needs to be attached to the dialogue/text that's displayed as it is the sound effect of the text appearing

    here's an example of the effect I'm looking for:


    would datasacks still be an appropriate method of managing this task?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You could also use an inputfield, it has a built in onchange event and you can just turn off the interactible part so players cant tap on it.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    If you had a ticka-ticka-ticka sound going in that box, then you could use a DSAudioVolume.cs script to control the volume.

    While the text is moving forward, set the volume to 1, then when the text stops scrolling, set it to 0.

    The DSAudioVolume.cs script uses the initial volume of the AudioSource(s) it finds as baseline, letting you adjust them in-scene or in-Prefab and still use 0 to 1 to control it.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You could also look at textmeshpro and the typewriter example.
     
  7. ey97

    ey97

    Joined:
    Jul 6, 2018
    Posts:
    3
    I had a look and I think input field does exactly the job I want but my issue now is that whenever I create one, my text is no longer displayed, as if it's covering the old text, or just not displaying it at all

    I'm assuming you set the text component to the object you want to populate the field?
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Inputfields have their own text that you need to set.

    Code (CSharp):
    1. public InputField target;
    2.  
    3. void Start()
    4. {
    5.     target.text = "Some text";
    6. }
    This text is set on the inputfield and not on the text component it targets. It will handle showing the text on the text component. If you try to set the text on the text component that the inputfield targets, it will get overwritten and thus shouldn't display.
     
    Last edited: Aug 3, 2018