0
\$\begingroup\$

I'm having a problem with my item selection script. As you can see in the video, button 3 (when the previous button is pressed) goes straight to button 1, instead of going to button 2 and then button 1. Can anyone help me with this issue! Thank you. My script is listed below;

int activeObject = 0;
public GameObject tool01, tool02, tool03, tool04, tool05, tool06;
public int max_number_of_objects;

// in the button press code
public void Right()
{

        // increment the active object, wrapping around to 0
        activeObject++;
        if (activeObject >= max_number_of_objects) {
            activeObject = 0;
        }
        // here's another way to do this that eliminates the conditional logic
        // just pick one of them - don't do them both
        // look up the modulus operator (%)
        activeObject = (activeObject + 1) % max_number_of_objects;
        RightActivateObject (activeObject);
}

public void Left()
{
    activeObject--;
    if (activeObject <= 0) {
        activeObject = max_number_of_objects;
    }
    // here's another way to do this that eliminates the conditional logic
    // just pick one of them - don't do them both
    // look up the modulus operator (%)
    activeObject = (activeObject - 1) % 1;
    LeftActivateObject (activeObject);
}
    // activate the selected object
void RightActivateObject(int activeObject)
{
    switch (activeObject)
    {
    case 0:
        tool01.SetActive(true);
        tool02.SetActive(false);
        tool03.SetActive(false);
        break;
    case 1:
        tool01.SetActive(false);
        tool02.SetActive(true);
        tool03.SetActive(false);
        break;
    case 2:
        tool01.SetActive(false);
        tool02.SetActive(false);
        tool03.SetActive(true);
        break;
    }
}

void LeftActivateObject(int activeObject)
{
    switch (activeObject)
    {
    case 0:
        tool04.SetActive(false);
        tool05.SetActive(true);
        tool06.SetActive(false);
        break;
    case 1:
        tool04.SetActive(false);
        tool05.SetActive(false);
        tool06.SetActive(true);
        break;
    case 2:
        tool04.SetActive(true);
        tool05.SetActive(false);
        tool06.SetActive(false);
        break;
    }
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

You don't need the tools4,5,6 and the modulus operator. That way you could merge both movements in a single method.

Example:

int activeObject = 0;
public GameObject tool01, tool02, tool03;
public int max_number_of_objects; //In this example is set to 3, 1 for each tool

// in the button press code
public void Right()
{

    // increment the active object, wrapping around to 0
    activeObject++;
    if (activeObject >= max_number_of_objects)
    {
        activeObject = 0;
    }
    ActivateObject();
}

public void Left()
{
    activeObject--;
    if (activeObject < 0)
    {
        activeObject = max_number_of_objects-1;
    }
    ActivateObject();
}
// activate the selected object
void ActivateObject()
{
    switch (activeObject)
    {
        case 0:
            tool01.SetActive(true);
            tool02.SetActive(false);
            tool03.SetActive(false);
            break;
        case 1:
            tool01.SetActive(false);
            tool02.SetActive(true);
            tool03.SetActive(false);
            break;
        case 2:
            tool01.SetActive(false);
            tool02.SetActive(false);
            tool03.SetActive(true);
            break;
    }
}

You could always have a list of elements, and use the count for the max_number_of_objects, but that's up to your implementation.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thank you!!!! It worked. I really appreciate it! :) Um do you know how not to loop it, once it get to the last button (button3)! \$\endgroup\$ Oct 29, 2016 at 7:40
  • \$\begingroup\$ You could try activeObject = (activeObject >= max_number_of_objects) ? max_number_of_objects - 1 : activeObject + 1 and then ActivateObject() for Right() and activeObject = (activeObject <= 0) ? 0 : activeObject - 1 and then ActivateObject() and then ActivateObject() for Left() using the conditional opertaor (?:) \$\endgroup\$
    – Daniel
    Oct 29, 2016 at 9:46
0
\$\begingroup\$

You need to try adding some sort of way of keeping the selection moving too far. Try putting all of the code that changes in a if statement tthat only if a boolean to be false.make it true at the end of the satement. then go into update to see if the statment is true, then have the program wait a certain amount of time to make it false again. I am not at a computer, so I can't give you an example.

\$\endgroup\$
1
  • \$\begingroup\$ I'm sorry could you explain it in more detail for me. Thank you for your suggestion, I really appreciate it. :) \$\endgroup\$ Oct 27, 2016 at 22:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .