- Home /
Code isn't running through
So I'm trying to make a Ahem Potato say something when the Player enters the Trigger and Left clicks on the Potato Game object (all of this is 2D). The Potato will ask a question and depending on the answer from the player the Potato will respond appropriately, and after the player answers the question the Potato will not ask any more questions as it marks as has talked to.
BUT
I have run into a problem the Potato won't talk when clicked on. The Potato game object is 0 on the Z axis and is set to trigger. I'm using C# and for the Life of me I cannot find out why. If you take time to fix this please explain what was wrong as I like to learn from mistakes.
using UnityEngine;
using System.Collections;
public class ProtatoTalk : MonoBehaviour {
public string [] answerButton;
public string [] Questions;
bool DisplayDialog = false;
bool HasTalkedTo = false;
void onGUI()
{
GUILayout.BeginArea (new Rect (1, 1, 400, 400));
if (DisplayDialog && !HasTalkedTo)
{
GUILayout.Label(Questions[0]);
if(GUILayout.Button (answerButton[0]))
{
GUILayout.Label (Questions[1]);
HasTalkedTo = true;
}
if(GUILayout.Button (answerButton[1]))
{
GUILayout.Label (Questions[2]);
HasTalkedTo = true;
}
}
GUILayout.Label (Questions[3]);
GUILayout.EndArea();
}
void OnTriggerEnter()
{
if(Input.GetMouseButtonDown(0))
DisplayDialog = true;
}
void OnTriggerExit()
{
DisplayDialog = false;
}
}
Answer by Bunnybomb7670 · Feb 23, 2014 at 12:20 AM
I think the issue is that you are calling you're code from OnTriggerEnter, if you think about it, that function is only called for one frame, you are expecting the player to collide with AND click in the same frame to call the dialog? I think you need to change it to OnTriggerStay because that is called every frame that the object stays with a collision.
$$anonymous$$ake sure there is a rigidbody attached to one of them otherwise it wont call the trigger.
Your answer
Follow this Question
Related Questions
UNITY Scripting - True and False not working! 3 Answers
Putting objects inside an empty GameObject changes the original position. 1 Answer
Custom Editor Window not opening after layout crash 1 Answer
NPC AI Unresponsive after spotting player once 2 Answers
Do I need an Apple developer account for Mac for my app to run properly? 0 Answers