- Home /
Dialogue box wont show in game unless it's in the canvas but...
Okay so, My games dialogue box wont show on the window unless I drag it into the canvas, problem with that being is the dialogue box will show upon starting up the game when I hit play . How do I get this thing to only appear when the user presses the space bar down near the box? https://youtu.be/SnA0uYLi5dU Here's a video of my problem so you can see first hand what I am dealing with
Answer by ForeignGod · Dec 20, 2016 at 10:04 AM
Keep the button in the canvas and disable it.
Add this script to a gameobject in the scene.
var yourButton : GameObject;
function Update (){
if(Input(GetKey(KeyCode.Space))
{
yourButton.SetActive (true);
}
}
Another good practice is to keep your game window to your desired build resolution/aspect ratio. This makes it much easier to design and place your UI elements.
Take a look at anchoring UI elements.
I'm sorry I'm probably doing this wrong could you have a look and see if I messed it up?
Sorry for a late answer, but my code was in unityscript, you could convert it or just create a new .js script :)
Answer by PizzaPie · Jan 03, 2017 at 02:01 PM
public class OpenDialog : MonoBehaviour
{
public GameObject boxDialogue;
public Transform player;
bool isActive;
public float someValue;
void Update()
{
if (Input.GetKey("space"))
{
float dist = Vector3.Distance(player.position, gameObject.transform.position);
if (!isActive && dist < someValue)
{
isActive = !isActive;
boxDialogue.SetActive(isActive);
}
else if(isActive)
{
isActive = !isActive;
boxDialogue.SetActive(isActive);
}
}
}
}
Attach that script to the box. Set the references on the inspector ,also set the someValue. It should work if i didn't mess with the if conditions. There are other ways to check if it is close to the box that is up to you
Your answer
![](https://koobas.hobune.stream/wayback/20220612103053im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Interacting with sign, canvas lags when activating. 0 Answers
UI menu not rendering properly 1 Answer
Unity Android Screen Glitch 1 Answer
[Video]Strange UI Behaviour, possible bug? 0 Answers
2D Collider Isnt working 1 Answer