Hide and Show Multiple Gameobjects upon button click .

I’m trying to develop an augmented reality application using Vuforia . I have to show specific Gameobject for an Image Target , say cube for Image Target 1 and Rectangle for Image target 2 , triangle for target 3 and so on for 10 targets. Both the targets will be tracked simultaneously . All i want to do is show cube first and when button is clicked it should hide the rectangle . and Again when button is clicked , it should show triangle hiding both cube and rectangle . All these with a single button and on multiple clicks . All i know is things are to be changed at Defaukttrackableeventhandler.cs script .

Whats i’m doing right now is each scene has a image target and a button for each scene , so when button is clicked , load next scene . I know this is not the right way to do . I’m new and i’m learning.

I think I understand what you are saying, so here is me just messing around.


Create a script like this and add it to an empty GameObject in your scene. Add your target objects to the array, preferably in order you wish to see them display after each click.

Also add a UI Button (via GameObject → UI → Button). On the button is an OnClick event. Drag the script onto this event. You can now select a function from the script in that box (click on the button that probably says No Function). You should see a list of scripts, find yours and then click on the function you want to call. In the example I made, it’s called OnClickButton.


It should default show the first one, then cycle through the rest. You should also disable the objects you are trying to show to be off. You can do this by default in the scene, clicking the checkbox at the top of each object, or cycle through the array OnStart() and do Setactive(false) to all of them.


public class Changer : MonoBehaviour
{
    [SerializeField] GameObject[] targets;

    GameObject currentTarget;
    int targetIndex;


    void Start()
    {
        ChangeTarget();
    }


    void ChangeTarget()
    {
        if (targets.Length > targetIndex)
        {
            if (currentTarget != null)
            {
                currentTarget.SetActive(false);
            }

            currentTarget = targets[targetIndex];
            currentTarget.SetActive(true);
            targetIndex++;
        }
    }


    public void OnClickButton()
    {
        ChangeTarget();
    }
}

Hi, @Vega4Life Thanks for your brilliant suggestion for this, I am a newbie to unity and tried some things. I used your script and nothing is happening. I guess because I don’t know how to add targets “Create a script like this and add it to an empty GameObject in your scene. Add your target objects to the array, preferably in order you wish to see them display after each click.”

I will be really thankful if you can drop a comment and teach me master /m\ ;),Hi, @Vega4Life Thanks for your brilliant suggestion for this, I am a newbie to unity and tried some things. I used your script and nothing is happening. I guess because i don’t know how to add targets “Create a script like this and add it to an empty GameObject in your scene. Add your target objects to the array, preferably in order you wish to see them display after each click.”

I will be really thankful if you can drop a comment on teach me master /m\ :wink: