Hey all,
I need some help on writing a JavaScript that will display a GUI Button when my character talks to an NPC, and is hidden when I don’t need it anymore. Right now, I have it so that the NPC automatically starts talking when the ray coming from the player collides with the NPC.
Ideally, I want to be able to have a conversation with the NPC, be able to choose a response with a GUI button, and have the NPC respond differently depending on the player’s choice. Right now, the NPC’s dialogue is being displayed via GUI Text. I have two code files:
NPCDialogue.js
static var characterSpeaking : boolean = false;
static var dialogue : String;
function OnGUI() {
(GUI.Button(Rect(10,70,50,30),"Click"));
}
function Start(){
characterSpeaking = false;
guiText.text = "";
}
function Update () {
if(characterSpeaking){
guiText.enabled = true;
guiText.text = dialogue;
}
}
and PlayerCollideWithNPC.js
function Update () {
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, 5)) {
if(hit.collider.gameObject.tag=="NPC"){
NPCDialogue.dialogue = "Hi there! I'm an NPC!";
NPCDialogue.characterSpeaking = true;
}
}
}
Can someone help me work with my existing code so that I can hide/show the GUI Button only when the NPC is speaking? Also, tips on creating a branching conversation (Displaying certain text when a certain button is pressed) would be much appreciated. Thanks in advance!
Awesome! That looks great! Thank you! Now how I can refer to this button when I give it logic for being clicked? I read somewhere that encapsulating the GUI Button in an if statement like this:
if GUI.Button(Rect(10,70,50,30),“Click”) {
//insert logic here
}
is like saying “When this button is clicked, do this”. But I doubt nesting the if-statements would work here. So how can I now make the button do something when clicked?
Hmm I suppose I haven’t but I don’t see how it would work. It’s like saying: “If the NPC is speaking, If you click on the button, do this” … Don’t you have to instantiate the button before having an if statement about whether the user clicked on it?
nope… a GUI.Button is just code that checks if a condition is satisfied (the button was clicked) just like any other if statement, it just happens to put a representation of that on the screen; its not an object or anything. That’s like saying, “you can’t put code in an if statement inside an if statement unless the condition of the outer if statement is satisfied”. You would never have any code!
Anyway, this will work fine:
function OnGUI() {
if(characterSpeaking){
if(GUI.Button(Rect(10,70,50,30),"Click")){
//do stuff
}
}
}
@The OP: Just assuming something that’s been suggested won’t work probably isn’t the best idea (especially if you’re new to Unity and/or programming yourself). At the very least, you can try it and see what happens.
But yes, it is like saying, ‘if the NPC is speaking, and if you click the button, do this’, and that’s fine.
As for instantiating the button, it’s the call to Button() itself that ‘instantiates’ it, so no, you don’t have to create it prior to the ‘if’ statement. (Remember, the built-in GUI system is immediate-mode, which means that controls only exist when you create them, and they only exist for that series of calls to OnGUI().)
Okay awesome! Thank you for the information! Yeah, you’re right, I could have tried it. I guess I wasn’t feeling very adventurous today, haha! I appreciate all your help!