• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by khrysller · Jun 10, 2019 at 02:37 PM · arrays

Show gameobject on tap hide on untap using arrays

Hi.

I am building an Trophy area for my game and I am looking for some help. I am trying to achieve the following:

When tap an image show the description and when this image is untaped hide the description

I created the Trophies.cs and attached it to the scene.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Trophies : MonoBehaviour
 {
   
     public GameObject[] TrophiesImages;
     public GameObject[] TrophiesDescription;
   
     void Start()
     {
         for (int i = 0; i < TrophiesDescription.Length; i++)
         {
             TrophiesDescription[i].SetActive(true);
         }
     }
 }

But how to create a dinamic code to auto control the action without doing it one by one? I was thinking about in doing one action for each trophy and do SetActive to true and false.... but I would like some light in doing it dinamicaly.

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · Jun 10, 2019 at 03:56 PM

Supposing the trophies are UI elements:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class Trophies : MonoBehaviour
 {
     public GameObject[] TrophiesImages;
     public GameObject[] TrophiesDescription;
 
     void Start()
     {
         for ( int i = 0 ; i < TrophiesDescription.Length ; i++ )
         {
             AddEventTrigger( TrophiesImages[i], TrophiesDescription[i] );
             TrophiesDescription[i].SetActive( false );
         }
     }
 
     private void AddEventTrigger( GameObject trophy, GameObject description )
     {
         EventTrigger trigger = trophy.AddComponent<EventTrigger>();
 
         // Pointer down => show
         EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
         pointerDownEntry.eventID = EventTriggerType.PointerDown;
         pointerDownEntry.callback.AddListener( data => ShowDescription( description ) );
         trigger.triggers.Add( pointerDownEntry );
 
         // Pointer up => hide
         EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
         pointerUpEntry.eventID = EventTriggerType.PointerUp;
         pointerUpEntry.callback.AddListener( data => HideDescription( description ) );
         trigger.triggers.Add( pointerUpEntry );
     }
 
     private void ShowDescription( GameObject description )
     {
         for ( int i = 0 ; i < TrophiesDescription.Length ; ++i )
             TrophiesDescription[i].SetActive( false );
 
         description.SetActive( true );
     }
 
     private void HideDescription( GameObject description )
     {
         description.SetActive( false );
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image khrysller · Jun 10, 2019 at 05:08 PM 0
Share

alt text

Thanks... I tried use this but it has no effect. The image show how I set up it... can you please help me again telling me if this is right? The trophies images are actually buttons...

thanks again

capture.png (20.1 kB)
avatar image Hellium khrysller · Jun 10, 2019 at 05:20 PM 0
Share

The trophies should not be buttons, just elements receiving clicks (Image / Text with RaycastTarget set to true). Have you an EventSystem in your scene? A GraphicRaycaster?

avatar image khrysller · Jun 10, 2019 at 05:51 PM 0
Share

Yes..alt text

Sorry I am learning unity yep... I might be missing something... alt text

2.png (11.3 kB)
avatar image Hellium khrysller · Jun 10, 2019 at 06:02 PM 0
Share

This should work (here, it works perfectly fine).


When you start the game, do the TrophiesImages have an EvenTrigger component?


$$anonymous$$ake sure the EventSystem detect your images. See following answer to check this:

https://answers.unity.com/questions/1402544/so-my-buttons-stop-working.html#answer-container-1402559

avatar image khrysller · Jun 10, 2019 at 06:58 PM 0
Share

alt text

It is... Sorry for the long conversation... but I dunno why it is not working

3.png (11.6 kB)
avatar image Hellium khrysller · Jun 10, 2019 at 07:17 PM 0
Share

Have you tried to put a Debug.Log inside the ShowDescription and HideDescription to see if the functions are called?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

178 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does this loop throw an error if int questComplete rises above 1? 0 Answers

Managing In-Game Variables of Different Data Types 1 Answer

Defining a number of gameobjects from another gameobject 0 Answers

animation array trying to play all at once 0 Answers

Noob question about very simple array script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges