- Home /
2 Objects spawning instead of one at each update
Hi everyone. I googled everything I can find but to no avail. I want the game to spawn a missile each time the player touches the screen.
But it's spawning 2 missiles at once with each touch. It doesn't look like there are 2 missiles and I could just roll with it. But what kind of programmer would I be if I can't solve this problem?
I put some debugging logs in each conditional statement and they do count twice with each touch. I'm now at the end of my wits.
#pragma strict
var missileObject : GameObject;
private var launcherReady : boolean;
function Start(){
launcherReady = true;
}
function Update () {
if( Input.touchCount == 1){
for (var touch : Touch in Input.touches){
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100) && launcherReady) {
if(touch.phase == TouchPhase.Ended) {
launcherReady = false;
var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0);
var v3 : Vector3 = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z - 0.5));
var mObject = Instantiate( missileObject, v3, Quaternion.Euler(270, 0, 0));
mObject.GetComponent(MissileMovement).yMovement = 1;
}
}
}
}
launcherReady = true;
}
Answer by Noob_Vulcan · May 05, 2014 at 07:04 AM
You can solve this issue using a bool .....
if(spawn){
var mObject = Instantiate( missileObject, v3, Quaternion.Euler(270, 0, 0));
spawn=false;
}
Make this spawn bool true when the user touches i.e touchPhase==Touch.Began or where you are detecting touch
Answer by johnq1216 · May 05, 2014 at 06:57 PM
Thanks Noob_Vulcan but that didn't work. I figured it out though. There's another object that has the same script. This happened to me before. Why didn't I see this at first?
Your answer
Follow this Question
Related Questions
Can you get the height that you are holding the phone? 2 Answers
big terrain performance && mobile 1 Answer
Touches not registering in android? 0 Answers
iOS and Android Publishing 2 Answers