Speech bubbles
Hello,
It may be a very basic question, but I'm completely stuck on it: I'm trying to make several speech bubbles appear on the NPC every time the player presses E. So far I was trying with instantiate, but every single bubble appears at the same time as soon as the player presses E.
Anyone can help?
Please post your code so that we can understand what the problem is.
$$anonymous$$y first guess would be that you're using Input.Get$$anonymous$$ey ins$$anonymous$$d of Input.Get$$anonymous$$eyDown, but without more information from your end this is only speculation.
Even with the Input.Get$$anonymous$$eyDown the same problem occurs, I'm either doing something completely not appropriate for this or there's a mistake somewhere
This is the code I was trying to work with:
void Update () {
float dx = player.transform.position.x - bubble.transform.position.x;
float dy = player.transform.position.y+1 - bubble.transform.position.y;
float dist = $$anonymous$$athf.Sqrt(dx*dx+dy*dy);
if (dist < 1.0f)
{
rend.enabled = true;
} else
{
rend.enabled = false;
}
if (dist < 1.0f && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
Destroy (bubble);
Instantiate(bubbleBribe, new Vector3(-5.23f, -1.92f, 0), Quaternion.identity);
}
if (dist < 1.0f && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
Destroy (bubbleBribe);
Instantiate(bubbleStory, new Vector3(-6.4f, -1.63f, 0), Quaternion.identity);
}
}
Answer by corn · Jan 19, 2016 at 02:53 PM
Your problem is right there :
if (dist < 1.0f && Input.GetKeyDown(KeyCode.E))
{
Destroy(bubble);
Instantiate(bubbleBribe, new Vector3(-5.23f, -1.92f, 0), Quaternion.identity);
}
if (dist < 1.0f && Input.GetKeyDown(KeyCode.E))
{
Destroy(bubbleBribe);
Instantiate(bubbleStory, new Vector3(-6.4f, -1.63f, 0), Quaternion.identity);
}
You use the exact same condition twice, so obviously both blocks will be executed. You have to throw in an extra condition so that the first one will be executed the first time you hit E, and the second one will only be executed when you hit E again.
This should work :
[SerializeField]
GameObject bubblePrefab;
[SerializeField]
GameObject bubbleBribePrefab;
[SerializeField]
GameObject bubbleStoryPrefab;
GameObject bubble;
GameObject bubbleBribe;
GameObject bubbleStory;
void Update()
{
float dx = player.transform.position.x - bubble.transform.position.x;
float dy = player.transform.position.y + 1 - bubble.transform.position.y;
float dist = Mathf.Sqrt(dx * dx + dy * dy);
if (dist < 1.0f)
{
rend.enabled = true;
}
else
{
rend.enabled = false;
}
// If bubble still exists, then when we hit E, show the bribe bubble
if (dist < 1.0f && Input.GetKeyDown(KeyCode.E) && bubble != null)
{
Destroy(bubble);
bubbleBribe = (GameObject) Instantiate(bubbleBribePrefab, new Vector3(-5.23f, -1.92f, 0), Quaternion.identity);
// Return now, so that the next block won't be exectuted
return;
}
// If the bribe bubble still exists, then when we hit E, show the story bubble
if (dist < 1.0f && Input.GetKeyDown(KeyCode.E) && bubbleBribe != null)
{
Destroy(bubbleBribe);
bubbleStory = (GameObject) Instantiate(bubbleStoryPrefab, new Vector3(-6.4f, -1.63f, 0), Quaternion.identity);
}
}
However, keep in mind that this design is quite flawed. With this fix, the script should work, but you should consider writing a ShowNextBubble method. You could create a Serializable struct with a GameObject (prefab) field and a Vector3 field, so that you can define all your bubbles in the inspector, and iterate through an array in your code.
You should always avoid having hard-coded values, always try to make your code as generic as possible. As it is, it would be a pain to make dialogs, you'd have to write a different script every time. Try to make one generic script that you could use for every dialog in your game, that will save you a lot of time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060434im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fix soooo hight jump 1 Answer
Adding small acceleration to 2d top down movement game? 0 Answers
,2d gameObject unwanted outline 0 Answers
how to make my 2d top down character face the direction of the cursor 1 Answer
How to make 2D Sprite Shadow? 0 Answers