0
\$\begingroup\$

I have a five game objects (ornament1, ornament2, ornament3, ornament4 and ornament5) and two buttons ("Next" and "Previous"). When I click "Next", the object should change from ornament1 to ornament2. When I click "Next", again, it should change from ornament2to ornament3; when I then click "Previous" it should change from ornament3to ornament2.

I have the "Next" button working, but the "Previous" button is not working. How do I change the game object on a button click?


Here is my code:

if(GUI.Button(new Rect(10, 20, 80, 50), "Ornament1"))
{
    Debug.Log("hhhhhhhhhhhhhhhhhhhhhhh" + countbool);

    if(count==0)
    {
        Debug.Log("hhhhhhhhhhhhhhhhhhhhhhh");
        ornament1.transform.position = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);
        ornament1.SetActive(true);
        ornament2.SetActive(false);
        ornament3.SetActive(false);
        ornament4.SetActive(false);
        ornament5.SetActive(false);
        count++;
    }
    else if(count == 1)
    {
        ornament2.transform.position = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);
        ornament2.SetActive(true);
        ornament1.SetActive(false);
        ornament3.SetActive(false);
        ornament4.SetActive(false);
        ornament5.SetActive(false);
        count++;
    }
    else if(count == 2)
    {   
        ornament3.transform.position = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);
        ornament3.SetActive(true);
        ornament1.SetActive(false);
        ornament2.SetActive(false);
        ornament4.SetActive(false);
        ornament5.SetActive(false);
        count++;
    }
    else if(count == 3)
    {
        ornament4.transform.position = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);
        ornament4.SetActive(true);
        ornament3.SetActive(false);
        ornament1.SetActive(false);
        ornament2.SetActive(false);
        ornament5.SetActive(false);
        count++;
    }
    else if(count == 4)
    {
        //ornament1.SetActive(true);
        ornament5.transform.position = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);
        ornament5.SetActive(true);
        ornament3.SetActive(false);
        ornament1.SetActive(false);
        ornament2.SetActive(false);
        ornament4.SetActive(false);
        count++;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Try and make all of them IF statements, not ELSE IF. \$\endgroup\$ Sep 16 '15 at 13:54
  • 2
    \$\begingroup\$ in previous button do you use count-- instead of count++ \$\endgroup\$ Sep 16 '15 at 13:59
  • \$\begingroup\$ after giving count-- still its not working \$\endgroup\$ Sep 17 '15 at 3:13
2
\$\begingroup\$

We know that your "Next" button works; at least, mostly. As such, we simply need to expand on your base code. That said, there are a few changes I make:

  • I move all game objects into an array. Given the nature in which you are using them, this makes more logical sense. We can also directly access them using an integer index, where we could not, if we simply included the number in the actual name.
  • I change the name of count to currentOrnamentID, as this value appears to point to the current game object, which does not really make sense as a "count".
  • I store originPosition as a class member. That way, if you ever need to change this position, you change it once. Furthermore, I believe it might be a little lighter on the garbage collecting, by passing values into an already-existing Vector3 instead of creating a new one.
  • In your last if statement, that is, where count == 4, you still increment count. In such cases, you can end up with logic errors, as you are effectively moving on to a game object that does not exist (remember, you say that you only have 5 game objects). I have adjusted the logic to overlap; selecting "Next" on the last game object will return you to the first game object.
  • You were previously resetting the SetActive for every game object, with each selection. This is unnecessary, as we should know what the last game object was. We only need to set the previous game object to SetActive(false).

As such, you can see the logic, below. All you need to do is call Next() from the "Next" button, and Previous() from the "Previous" button. We then take the currentOrnamentID, and use it to determine what game object is currently displayed. We adjust this value, accordingly, before we send the new currentOrnmanetID and the previous currentOrnamentID by value to a EnableOrnament method. You will notice we pass a lot of direct values in; this is just a semantic difference in efficiency. We already know the exact values that should be passed, both to the new currentOrnamentID and EnableOrnament, given the specific context. No need to "work it out" again, by calling to the actual variables or performing ++.

int currentOrnamentID = 0;
public GameObject[] ornaments;
public Vector3 originPosition = new Vector3(-0.1428955f, -0.1894745f, 4.108555f);

private void Next
{
    switch(currentOrnamentID)
    {
        case 0:
            currentOrnamentID = 1;
            EnableOrnament(1, 0);
            break;
        case 1:
            currentOrnamentID = 2;
            EnableOrnament(2, 1);
            break;
        case 2:
            currentOrnamentID = 3;
            EnableOrnament(3, 2);
            break;
        case 3:
            currentOrnamentID = 4;
            EnableOrnament(4, 3);
            break;
        case 4:
            currentOrnamentID = 0;
            EnableOrnament(0, 4);
            break;
    }
}

private void Previous
{
    switch(currentOrnamentID)
    {
        case 0:
            currentOrnamentID = 4;
            EnableOrnament(4, 0);
            break;
        case 1:
            currentOrnamentID = 0;
            EnableOrnament(0, 1);
            break;
        case 2:
            currentOrnamentID = 1;
            EnableOrnament(1, 2);
            break;
        case 3:
            currentOrnamentID = 2;
            EnableOrnament(2, 3);
            break;
        case 4:
            currentOrnamentID = 3;
            EnableOrnament(3, 4);
            break;
    }
}

private void EnableOrnament(int newID, int previousID)
{
    ornaments[previousID].SetActive(false);
    ornaments[newID].SetActive(true);

    ornaments[newID].transform.position = originPosition;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.