after pressing button many times the player starts moving more.
Hello, I am pretty new to unity and I am currently making my first game. So today i made two buttons which control the player. One button is for moving up and the other one is for moving down. I made a script for these buttons and my problem is that if I press one of the buttons more than one time then the player starts moving more. (Sorry for my bad english). So if i press the button second time then the vector 3 that should be playyer.transform.position += new Vector3 (0,-0.2f* Time.deltaTime ,0); it goes to something like that playyer.transform.position += new Vector3 (0,-2f* Time.deltaTime ,0); , the y increases every time i press the button.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class proov : MonoBehaviour {
public Button ules;
public GameObject playyer;
public Button alla;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
ules.onClick.AddListener (() => {
playyer.transform.position += new Vector3 (0, 0.2f *Time.deltaTime , 0);
});
alla.onClick.AddListener (() => {
playyer.transform.position += new Vector3 (0,-0.2f* Time.deltaTime ,0);
});
}
}
Thanks for reading and sorry for my bad English.
Answer by haruna9x · Aug 18, 2017 at 02:09 PM
You are repeating the event registration, you should only register once in OnEnable(), you also need to cancel the registration in OnDisable().