Set Active Three objects?

Hi everyone,

I’m trying to show one object at a time between three, every time I press one button.

Now I’m using to activate it the function “SetActive”, with a static var from another script.

But now I can only switch between two objects…

Please take a look to this little short video.

Link Video

Please Take a look to my Script.

Thanks for you help.

var tool01 : GameObject;
var tool02 : GameObject;
var tool03 : GameObject;
 

function Update () {
if(animations .changeGo == 1){ 
tool01.SetActive(!tool01.activeInHierarchy);
tool02.SetActive(!tool02.activeInHierarchy);
tool03.SetActive(!tool03.activeInHierarchy);
animations . changeGo = 0;
    }
}

Set everybody inactive, then activate the one you actually want active. Brutal, but efficient.

If I’m understanding right, you’re trying to activate objects in sequence, the next one activated each time you press a button, looping back to the first once you’ve activated all 3, and only one active at a time? Kind of like radio buttons?

One way to do this is to have an integer value that identifies which item is active. Each time you press the button, increment the value by 1. Once you’ve reached the highest value, set the value back to 0.

I’m going to do the code examples in a compbinatoin of C# and pseudocode…

// declare in the class, or at the top of the script in Javascript
int activeObject = 0;

// in the button press code
if (button is pressed)
{
  // 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;

  ActivateObject(activeObject);
}

// activate the selected object
void ActivateObject(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;
  }
}

Or something like that. A better way to deal with the 3 objects would be to store them in an array and use activeObject as an index into the array. You can activate that one and deactivate all the others that way with simple function that will handle any number of objects.

well i think this is little too late but

public class TestGun : MonoBehaviour
{

public GameObject[] gun;
public int activeIndex = 0;

public void SetActiveObject(int aIndex)
{
    activeIndex = aIndex;
    for (int i = 0; i < gun.Length; i++)
        gun*.SetActive(i == activeIndex);*

}
void Update()
{
SetActiveObject(activeIndex);
}
public void UpgradeButtom()
{
gun[activeIndex].SetActive(true);
activeIndex++;
}
}