Instantiate limiting

hey everyone,

im using the following code

private var gui :GUITexture;
function Start(){
	gui=GetComponent(GUITexture);
}

var chair:Transform;

function Update(){

var count = Input.touchCount;

for (var i: int =0; i<count; i++){
	var touch :Touch = Input.GetTouch(i);
	if (gui.HitTest(touch.position)){
		
		Instantiate(chair, transform.position, transform.rotation);
		
	}
}
}

everything works great besides that when i tap the GUI it creates 1 chair per frame that i am holding the GUI down. I know this is because of the Update function, but i cant seem to come about a good way of making it only create 1 chair when i touch the GUI texture

I tried adding in the .phase and touchphase.began but i must not be adding it correctly… any ideas?

Hello,

You have to handle this with a variable, let’s say V1.

When TouchPhase == Begin,
if V1 is false
you instantiate and change the value of your variable V1 to true
end if

When TouchPhase == End
V1 = false

This way, you will instantiate only when a new finger will press the screen.

Then you will have to handle the array of touch. It depends on what you want to achieve…

good luck !

Hervé

I use:

if (Input.GetButtonDown ("Fire1")  guiTexture.HitTest (Input.touches[0].position))

Unity iPhone accepts the mouse button code for touches.

You don’t need an extra variable. Just require that the touch phase is Begin:

if (touch.phase == TouchPhase.Began  gui.HitTest(touch.position)){

ok thank you i will try it shortly, i was unaware i was able to use in the script… sucha smartyy :wink: lol