Good evening everyone, I’m following a tutorial on youtube to create a dialogue, I’m almost at the end and everything is ok.
Except for a small problem that I can’t solve, there are two texts that have to appear a few seconds apart but once I click play, the texts are displayed together, as it appears in the attached picture.
I have already checked the scripts and settings and everything seems to be ok so I really don’t understand what this error could be due to.
The video in question is this one, maybe someone has already seen it:
You mention the scripts seem to be okay, but can you share anyways? Please use code tags from the text ribbon.
These are the 4 scripts
1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
public static SoundManager instance { get; private set; }
private AudioSource source;
private void Awake()
{
instance = this;
source = GetComponent<AudioSource>();
}
public void PlaySound(AudioClip sound)
{
source.PlayOneShot(sound);
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace DialogueSystem
{
public class DialogueBaseClass : MonoBehaviour
{
public bool finished { get; private set; }
protected IEnumerator WriteText(string input, Text textHolder, Color textColor, Font textFont, float delay, AudioClip sound, float delayBetweenLines)
{
textHolder.color = textColor;
textHolder.font = textFont;
for(int i=0; i<input.Length; i++)
{
textHolder.text += input[i];
//suoni lettere
SoundManager.instance.PlaySound(sound);
yield return new WaitForSeconds(delay);
}
yield return new WaitForSeconds(delayBetweenLines);
finished = true;
}
}
}
using System.Collections;
using UnityEngine;
namespace DialogueSystem
{
public class DialogueHolder : MonoBehaviour
{
private void Awake()
{
StartCoroutine(dialogueSequence());
}
private IEnumerator dialogueSequence()
{
for(int i = 0; i < transform.childCount; i++)
{
Deactivate();
transform.GetChild(i).gameObject.SetActive(true);
yield return new WaitUntil(() => transform.GetChild(i).GetComponent<DialogueLine>().finished);
}
}
private void Deactivate()
{
for(int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
}
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace DialogueSystem
{
public class DialogueLine : DialogueBaseClass
{
private Text textHolder;
[Header ("Text Options")]
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private Font textFont;
[Header("Time Parameters")]
[SerializeField] private float delay;
[SerializeField] private float delayBetweenLines;
[Header("Sound")]
[SerializeField] private AudioClip sound;
[Header("Character Image")]
[SerializeField] private Sprite characterSprite;
[SerializeField] private Image imageHolder;
private void Awake()
{
textHolder = GetComponent<Text>();
textHolder.text = "";
imageHolder.sprite = characterSprite;
imageHolder.preserveAspect = true;
}
private void Start()
{
StartCoroutine(WriteText(input, textHolder, textColor, textFont, delay, sound, delayBetweenLines));
}
}
}
Is this issue occurring with every text that shows up or just on certain parts of the dialogue?
With each text that appears
So the code looks pretty good, only thing is if it is attached twice or 2 different objects. But i suspect the issue has to do with your active gameobjects. From your Hierarchy you have 2 Dialogue Line objects but they arent both under the same parent of dialogue holder which seems weird.
I don’t know, that’s what the person in the video did and he did it well.
I followed the same steps, I’m pretty sure.
Well start debugging them then. Run the game with one of the DialogueLines active and the other not. Then switch. See what happens. If there are scripts on them, maybe even try disabling those one at a time.
rarac
December 9, 2021, 4:57pm
9
abdallaelawad:
I don’t know, that’s what the person in the video did and he did it well.
I followed the same steps, I’m pretty sure.
corny sam is right, your hierarchy is messed up you didnt do it like the tutorial
The creator of the video helped me out, the problem was simply that the script was not linked to the object.
Thank you all very much for your advice, have a good evening.