Hi everyone , im developing a simple AR
How to enable visibility of an Object and Hide visibility of another object upon pressing a single button ?
So when image target is found , First 3d object will be shown and when button is pressed , Second 3d object will be shown and first one will hide .
If you want to avoid custom scripting, you can go with this setup:
- Create 2 UI Toggles
- For each Toggle in OnValueChanged Event select desired target GameObject, select SetActive dynamic method
- Add Toggle Group component to the parent of 2 toggles
- For each Toggle reference this ToggleGroup in Group
Now every time you check the other toggle, the GameObject it commands will become “active” (visibile), and another toggle in group will become unchecked and therefore its commanded GameObject will become “non active” (invisibile).
You could try enabling/disabling the 3D objects.
public gameObject object1;
public gameObject object2;
public void buttonClicked(){
object1.setActive(false); //deactivates first object
object2.setActive(true); //activates second object
}
The following script must be attached to only one gameObject (your button for instance)
// Drag & Drop your gameObjects here
public GameObject[] GameObjectsList ;
private int shownGameObjectIndex = -1 ;
private void Start()
{
for( int i = 0 ; i < GameObjectsList.Length ; ++i )
GameObjectsList*.SetActive( false ) ;*
SelectNextGameObject();
}
// Add this function as callback for your button’s onClick event
public void SelectNextGameObject()
{
int index = shownGameObjectIndex >= GameObjectsList.Length - 1 ? -1 : shownGameObjectIndex ;
SelectGameObject( index + 1 );
}
public void SelectPreviousGameObject()
{
int index = shownGameObjectIndex <= 0 ? GameObjectsList.Length : shownGameObjectIndex ;
SelectGameObject( index - 1 );
}
public void SelectGameObject( int index )
{
if ( shownGameObjectIndex >= 0 )
GameObjectsList[shownGameObjectIndex].SetActive( false );
shownGameObjectIndex = index;
GameObjectsList[shownGameObjectIndex].SetActive( true );
}
----
If you want to use a toggle (so if you want to have only 2 gameObjects)
public GameObject GameObject1;
public GameObject GameObject2;
// Add the following function to the toggle’s event
public void OnToggleValueChanged( bool value )
{
GameObject1.SetActive( value ) ;
GameObject2.SetActive( !value ) ;
}