- Home /
Error when using GUITexture.HitTest
Hi
I'm trying to use GUI.Texture.HitTest(touch.position) to get the true or false bool when user touches the mobile screen. I get the following error message: "BCE0019: 'Hit Test' is not a member of UnityEngine.GameObject."
Here's my code:
var GUI_LeftArrow : GameObject;
var GUI_RightArrow : GameObject;
function Awake() {
GUI_LeftArrow = GameObject.Find("GUI_LeftArrow");
GUI_RightArrow = GameObject.Find("GUI_RightArrow");
}
function Update() {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position)) {
Debug.Log("Left Arrow Down");
}
}
}
GameObject.Find is getting the GUITexture (Game Object) from the hierarchy panel in Unity. Maybe I'm missing an import into the script at the top? Any help will be great.
Answer by aldonaletto · Oct 03, 2013 at 03:44 AM
Unity is right! HitTest is member of GUITexture, not of GameObject - you should use the guiTexture property of GUI_LeftArrow instead:
if (... && GUI_LeftArrow.guiTexture.HitTest(touch.position)){
NOTE: never use Find inside Awake - the objects you're looking for may have not been created yet. Rename the function Awake as Start and potential headaches will be avoided.
Answer by TrickyHandz · Oct 03, 2013 at 04:02 AM
GameObject doesn't have a HitTest...but GUITexture does. So, you need to declare your variables a GUITexture not GameObject. Then you need to get the guiTexture property of the GameObject found in the Start Function as a GUITexture. Give this a try:
var GUI_LeftArrow : GUITexture;
var GUI_RightArrow : GUITexture;
function Start() {
GUI_LeftArrow = GameObject.Find("GUI_LeftArrow").guiTexture;
GUI_RightArrow = GameObject.Find("GUI_RightArrow").guiTexture;
}
function Update()
{
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position))
{
Debug.Log("Left Arrow Down");
}
}
}
Thank you so much TrickyHandz. This is the solution. Appreciate your help. :-)
No problem. Just mark the answer as accepted when you have a chance. Happy coding!
Sure, I'd love to do that. Just not sure where or if I have to have reputation points to be able to upvote or mark as accepted...let me know.
Answer by Rick.Miranda · Oct 03, 2013 at 10:41 PM
Final Working Solution for detecting GUITexture touch events:
var GUI_LeftArrow : GUITexture;
var GUI_RightArrow : GUITexture;
function Start() {
GUI_LeftArrow = GameObject.Find("GUI_LeftArrow").guiTexture;
GUI_RightArrow = GameObject.Find("GUI_RightArrow").guiTexture;
}
function Update() {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position)) {
//if (touch.phase == TouchPhase.Stationary) {
//if (GUI_LeftArrow.HitTest(touch.position)) {
Debug.Log("Left Arrow Down");
}
}
}
Your answer
Follow this Question
Related Questions
MouseUp not working on Mobile build 0 Answers
FPS touch bug in Asset Sample Beta? 0 Answers
Mobile Input Not Accurate 0 Answers
Touch not working on guiTexture. 1 Answer
Moving an object in Unity3d for Android 0 Answers