- Home /
Dialogue OnTriggerEnter2D not working
So I've imported the asset "Dialoguer" and I'm trying to make it so that a gui button will appear to begin the conversation once I enter a trigger in a 2D game but it's not working to far. I have this script which I put on a 2D box collider:
using UnityEngine;
using System.Collections;
public class DialoguerExampleStart : MonoBehaviour {
void Awake(){
// You must initialize Dialoguer before using it!
Dialoguer.Initialize();
}
void OnTriggerEnter2D (other: Collider2D){
void OnGUI(){
if(GUI.Button (new Rect(10,10,100,30), "Start!")){
Dialoguer.StartDialogue(3);
}
}
}
}
And I'm not sure how to edit it so that it works that way. I know this is completely wrong but I'm very new to coding so I'm not sure how it's wrong or how to fix it. :( Any help would be much appreciated.
I get the errors: Assets/DialoguerExamples/_ExampleScene/Classes/DialoguerExampleStart.cs(18,9): error CS8025: Parsing error
Assets/DialoguerExamples/_ExampleScene/Classes/DialoguerExampleStart.cs(11,19): error CS1525: Unexpected symbol (', expecting )', ,', ;', [', or ='
Assets/DialoguerExamples/_ExampleScene/Classes/DialoguerExampleStart.cs(11,18): error CS1547: Keyword `void' cannot be used in this context
Assets/DialoguerExamples/_ExampleScene/Classes/DialoguerExampleStart.cs(10,32): error CS1041: Identifier expected
Answer by OctoSloths · Nov 29, 2014 at 03:16 AM
I figured it out! Here is the code in case anyone ends up needing it:
using UnityEngine;
using System.Collections;
public class DialoguerExampleStart : MonoBehaviour {
private bool bShowGUI = false;
void Awake(){
// You must initialize Dialoguer before using it!
Dialoguer.Initialize();
}
void OnTriggerEnter2D (Collider2D other){
bShowGUI = true;
}
void OnTriggerExit2D (Collider2D other) {
bShowGUI = false;
}
void OnGUI(){
if (bShowGUI) {
if (GUI.Button (new Rect (10, 10, 100, 30), "Investigate")) {
Dialoguer.StartDialogue (0);
}
}
}
}
Answer by spiceboy9994 · Nov 28, 2014 at 09:40 PM
Well the code is not correct from my point of view. Why do you have OnGUI Method within the OnTriggerEnter2D... separated those methods and you'll be fine
void OnTriggerEnter2D (other: Collider2D){
}
void OnGUI(){
if(GUI.Button (new Rect(10,10,100,30), "Start!")){
Dialoguer.StartDialogue(3);
}
}
Okay this allows the script to run but the GUIButton is visible without entering the trigger. I'm trying to make it so the button only appears after the player has entered a trigger.
Your answer