0

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)

2
  • I'm a little unclear what you mean by "but after you clicked the second time it's creating adding one more clone to other instead of one clone". Could you expand a bit? Also, if you click a third time, do you get three clones? Commented Aug 30, 2018 at 14:00
  • It keeps creating one more clone of the created clones. e.g if you click 3 times it creates 3 objects instead of one object. Commented Aug 30, 2018 at 14:04

2 Answers 2

2

I believe the problem is happening because you're adding a button click listener each time the mouse is clicked (in your Update function). The first time the mouse is clicked it adds the first listener during the Update, the 2nd time the mouse is clicked it adds another listener during the Update. So now when the mouse is clicked both of these listeners are triggered, adding 2 objects instead of 1. This pattern would repeat, i.e. clicking a 3rd time would cause it to add 3 objects now.

Move the AddListener function call into your Start function so that there is only one listener added, like so:

void Start () 
{
    positionX = transform.position.x;
    positionY = transform.position.y;

    btn1 = buttonUp.GetComponent<Button>();
    btn1.onClick.AddListener(UpClicked);
}

void Update() 
{
}
Sign up to request clarification or add additional context in comments.

Comments

1

The culprit is in your Update() Method:

void Update() 
{
    if (Input.GetMouseButtonDown(0))
    {
        btn1.onClick.AddListener(UpClicked);
    }
}

This adds another call to UpClickd() to the onClick event each time the mouse is clicked, which is probably not what you wanted.

Move the AddListener to the Start() function - so there is only one function call when the event occurs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.