There is one button in the game and if you click him, the button needs to create an object, but when you click it first, it's creating one clone, but after you clicked the second time it's creating adding one more clone to other instead of one clone.
public float positionX;
public float positionY;
public Button buttonUp;
Button btn1;
First, I created the variables.
void Start ()
{
positionX = transform.position.x;
positionY = transform.position.y;
btn1 = buttonUp.GetComponent<Button>();
}
Then assigned the position's values on Start Method and Button component
void Update()
{
if (Input.GetMouseButtonDown(0))
{
btn1.onClick.AddListener(UpClicked);
}
}
And controlled if a user clicks button set addListener
float[] CreatePlayerPartsUp(float pX, float pY)
{
float positionXFun = pX;
float positionYFun = pY;
GameObject part = Instantiate(playerParts, new Vector2(positionXFun, positionYFun + 16), Quaternion.identity);
float[] turnPosition = new float[2]{part.transform.position.x, part.transform.position.y};
return turnPosition;
}
After, I created a method that keeps the position of the object and returned them by using an array.
void UpClicked()
{
float[] createdPosition= CreatePlayerPartsUp(positionX, positionY);
positionX = createdPosition[0];
positionY = createdPosition[1];
}
Finally, I called the method and assigned the new position.
But the problem has occurred and I don't know why it happened. (First, create 1 but the other times it starts to create adding one more clone to other instead of 1)