#making text display when you click on an object

1 messages · Page 1 of 1 (latest)

gloomy crater
#

What I have now is a script that animates text, and a public array that stores all text in the scene.

What I need help with is finding a way to display only specific text when you click on a gameobject.

Eg. I want to click on the boltcutters, then display only boltcutter specific text, then close the UI textbox.

The way the script is set up now is really hard to do, I've tried to make a public array then have the main script inherit from the public array but that doesn't work.

Main Script:

using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;
public class typewriteeffect : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI typewriterEffect;
    [SerializeField] float timeBetweenCharacters;
    [SerializeField] float timeBetweenWords;
    public string[] stringArray;
    int i = 0;
    void Start()
    {

        EndCheck();
        
    }

    public void EndCheck()
    {
        if(i<= stringArray.Length -1)
        {
            typewriterEffect.text = stringArray[i];
            StartCoroutine(TextVisible());

        }
    }

    private IEnumerator TextVisible()
    {
        typewriterEffect.ForceMeshUpdate();
        int totalVisibleCharacters = typewriterEffect.textInfo.characterCount;
        int counter = 0;
        

        while (true)
        {
            int visibleCount = counter % (totalVisibleCharacters + 1);
            typewriterEffect.maxVisibleCharacters = visibleCount;
            
            if (visibleCount >= totalVisibleCharacters)
            {
                //Invoke("EndCheck", timeBetweenWords);
                i += 1;
                break;
            }
            counter += 1;
            yield return new WaitForSeconds(timeBetweenCharacters);


        }


    }
}
#

The only solution I can see is entering all the text needed per scene in the public array and manually making a switch statement that scrolls through every object in the scene, and pulls up the right array number and print the needed text.

This solution seems really inefficent though.

Eg. Boltcutter text is stored on array[2] and array[3], so the switch statement will run through only that line of text and close it.

I tried using tags, but I already use a tagging system to determine when my mouse cursor changes sprite.

#

The script breaks if the array is turned private because EndCheck() is tied to the button that lets the array advance,

meager idol
#

why would the text to display for bolt cutters not be on the bolt cutters, or at least the needed ids?

gloomy crater
#

its also impossible to resize the textbox

#

so its just huge

#

okay, somewhat managed to resize it, but its weird in different aspect ratios

still have an issue with this being an inefficent way to handle text 😦

meager idol
#

it sounds like you want to have the text box be a single instance, and have every object use the same one-

I'm not sure I understand why you can't have, say, a string[] displayText field on each individual object you want to display text, and when it's clicked call, e.g: TypeWriter.ShowText(displayText);

#

the text to display is properly associated with the object that gets clicked, and the text box/writer is generic and can display whatever is given to it from whatever gets clicked

#

the only complications to consider are

  1. how does each object reference the text box (but you're already doing this)
  2. how do you handle two objects trying to display things at the same (you need to resolve this one anyway)