I have a Canvas with an Image and a TextMeshPro (TMP) as a child, and the dialog's canvas component is set to false in the Start() method so as to hide it in the main Canvas. The TMP appears over the image (like a text inside a dialog box). I have a player and a coin sprite in a 2D environment. When the player picks up the coin, I try to display the dialog box and the TMP as shown below.
public class PlayerMovement : MonoBehaviour {
public GameObject suggestion;
public GameObject dialogBox;
private bool wasSuggestionShown; //to check if dialog was shown
private void Start()
{
wasSuggestionShown = false;
suggestionTimer = 0;
dialogBox.GetComponent<Canvas>().enabled = false; //To hide the dialog box
}
void Update () {
//horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input
if (wasSuggestionShown)
{
suggestionTimer += Time.deltaTime;
if (suggestionTimer > 5)
{
wasSuggestionShown = false;
dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
}
}
}
void FixedUpdate ()
{
// Move the character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
//For destroying coin object on collision with player
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("coin"))
{
//For destroying coin and to recude player movement speed.
Destroy(col.gameObject); //Coin Disappears
isRunSpeedReduced = true;
runSpeed = 10f;
//For Showing dialog box
dialogBox.GetComponent<Canvas>().enabled = true;
wasSuggestionShown = true;
}
}
}
In the OnTriggerEnter2D()
method, I check if the player character touches the coin and if so, I destroy the object and display the dialog box and TMP and hide them after 5 seconds. The problem is that
"When I include another coin, the same dialog box and TMP, do not show up when the player picks the second coin. Both the coins have the same tag 'coin' "
One may argue that if the script is in the same object which was destroyed or is inactive, then it is impossible. But I am doing all of this in the Player's movement script which is attached to the player Object.
Also, the way that I toggle the dialog box does not make a difference. Be it dialogBox.GetComponent<Canvas>().enabled = true;
or dialogBox.SetActive(true)
Either of these display only once and that is the first occurence.
Even if I want to instantiate it, I don't know the exact transforms to position it properly in the canvas. (I want it in the bottom middle part, like how it can be anchored)
Scene Hierarchy:
The issue is in your Update()
, where you turn off the canvas after suggestionTimer
ticks over:
void Update () {
//horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed; // for pc keys
horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed; //for smartphone input
if (wasSuggestionShown)
{
suggestionTimer += Time.deltaTime;
if (suggestionTimer > 5)
{
wasSuggestionShown = false;
dialogBox.GetComponent<Canvas>().enabled = false; //TO hide dialog box after displaying it
}
}
}
The cause is that you never reset the suggestionTimer
when you hit a new coin. Do this:
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("coin"))
{
//For destroying coin and to recude player movement speed.
Destroy(col.gameObject); //Coin Disappears
isRunSpeedReduced = true;
runSpeed = 10f;
//For Showing dialog box
dialogBox.GetComponent<Canvas>().enabled = true;
wasSuggestionShown = true;
// !!! ADD THIS
suggestionTimer = 0;
}
}