- Home /
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.
Answer by Vega4Life · Nov 01, 2018 at 03:17 PM
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, how can I reverse this? I have 2 buttons next and prev. I also have 4 images. It's working on next button the images works from 1 - 4 but I dont know what to do for previous button. Can you help me?
You just have to decrease the index ins$$anonymous$$d of increase it. So change the targetIndex++;
line to targetIndex--;
and that's it.
Answer by harimukulsagar · Jul 20, 2021 at 11:39 AM
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\ ;)
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Augmented Reality Tracker 1 Answer
How do ı acces dji matrice100 camera in my augmented reality application?(Using vuforia sdk) 0 Answers
Access vuforia ARCamera and put it on a sphere? 0 Answers
VUFORIA MULTI-TARGETS: SCRIPTING & CHILD HOLOGRAM VISUALIZATION 0 Answers