- Home /
iOS GUITexture not enabled still receives input
I'm using GUITextures for my buttons in an iOS app. They seem to function well (when tapped they do something) however I don't want them enabled or visible until the user taps the appropriate button to make them appear.
As of now I have a "color" button which when tapped makes a color picker appear for the user to select the desired color they would like to change on a given gameObject. The problem is in Start() I make the gameObject.active = false and each component.enabled = false which makes the GUITexture not visible however it still functions when the it's area is tapped.
Should I just instantiate the empty gameObject with the GUITexture component to avoid this? I would think there is a way for the gameObject to just be disables/not active however it is not working for me..
Here's the GUI script I'm using to detect touch and call to other scripts' functions..
var color : GUITexture; //empty gameObject with GUITexture component
var cp : GameObject; //ColorPicker gameObject with GUITexture and script components
function Start() {
cp.gameObject.active = false;
cp.GetComponent(colorPickerTest).enabled = false;
cp.GetComponent(GUITexture).enabled = false;
}
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount > 0) {
print ("User has " + fingerCount + " finger(s) touching the screen");
}
if (Input.touchCount>0)
{
for (var touch : Touch in Input.touches)
{
if(touch.phase == TouchPhase.Began && color.HitTest(touch.position))
{
cp.gameObject.active = true;
cp.GetComponent(colorPickerTest).enabled = true;
cp.GetComponent(GUITexture).enabled = true;
Debug.Log("TouchedColorBtn");
}
if(touch.phase == TouchPhase.Began && cs.GetComponent(GUITexture).HitTest(touch.position))
{
cs.GetComponent(ColorSwitch).colorPick();
Debug.Log("TouchColorPicker");
}
}
}
}
Answer by Seth-Bergman · Jul 19, 2012 at 07:17 AM
you could just use a flag instead:
for (var touch : Touch in Input.touches)
{
if(touch.phase == TouchPhase.Began && color.HitTest(touch.position))
{
enablePicker = true;
Debug.Log("TouchedColorBtn");
}
if(touch.phase == TouchPhase.Began && cs.GetComponent(GUITexture).HitTest(touch.position) && enablePicker)
{
cs.GetComponent(ColorSwitch).colorPick();
Debug.Log("TouchColorPicker");
}
}
I don't understand how this is using a flag, is it "enablePicker = true;" ?
yes that's what I meant, I could have said "boolean"
A boolean seems like it would work, however I've tried it along with disabling the GameObject with GameObject.enabled = false; or GameObject.component.enabled = false; or GameObject.SetActiveRecursively(true);
Although the GUITexture is not drawn, the GameObject with the GUITexture component is not active and yet it still is detecting taps with HitTest and functions are being called from scripts attached to the deactivated GameObject.... I'm kinda stumped here.
After writing all of that... I think I just fixed it. Ins$$anonymous$$d of just using:
if(touch.phase == TouchPhase.Began && cp.GetComponent(GUITexture).HitTest(touch.position)) {
//DoSomething
}
I added that the boolean must be set to true along with the HitTest:
if(touch.phase == TouchPhase.Began && enabledPicker == true && cp.GetComponent(GUITexture).HitTest(touch.position)) {
//DoSomething
}
This seems to be working for now.. I'll keep testing it out... Thanks again for all of your help!