- Home /
Why does Update() calls itself again and again?
I'm new in Unity gaming and making android game. I am implementing the menu screens before the game starts. I have a 15 objects on the screen that can be clicked to select the players i want in my game.
to touch the object i have separate script for each object, and I add that player name in my List when clicked. The problem is that if I click my object once, and then check the size of my List, it gives either 5 or 4 or even more than that. Is it because my Update() method is being called more than once? Please tell me what to do?
My script is as follows:
using UnityEngine;
using System.Collections;
public class midge : MonoBehaviour {
// Use this for initialization
void Start () {
}
void Update () {
if (Input.touchCount == 1)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
Material newMat = Resources.Load("New Material", typeof(Material)) as Material;
gameObject.renderer.material = newMat;
PlayersManager.objPlayerList.Add(PlayersManager.setPlayerObject("Midge"));
countausplayer.countteam++;
}
}
}
}
Please guide me where am I going wrong?? It's really urgent!!
Answer by HarshadK · Sep 01, 2014 at 01:01 PM
Update is called each frame.
So now your function will check if there is a touch for each frame and add your player to the list for the time your finger is on the screen.
You need:
if (Input.touchCount == 1 && touchPhase.Begin)
{
// Perform your action here
}
That. because your finger isn't fast enough to leave the screen before the cpu ends the frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162029im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Everytime I alter code during runtime my lists break 0 Answers
How to put buttons in a list? 1 Answer
Cloud recognition in Vuforia 0 Answers