• 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 /
avatar image
Question by Jadeon777 · Jun 11, 2013 at 08:49 PM · c#parentchildselection

Controlling GameObject selection across Parent/Child

Hello all, this is my first question here on UnityAnswers so please bare with me.

Currently I have a GameObject named Board

The Board is comprised of a 20x20 Grid of "Tile" Game objects. Each tile has a C# script that highlights when it is moused over.

I would also like the tiles to be select-able, in that they maintain their highlight if they are clicked by mouse (Additional functionality will be added to this later such as Menu pop ups and building animations).

My problem is a pretty basic one but searching the site I haven't found a solution, possibly because I am misunderstanding the issue and searching incorrectly.

I do not want more than one "tile" GameObject to be selected at a time.

I.E. If I have tile A selected, and I click tile B, tile B should assume the active selection highlight and tile A should deselect.

I am not sure how to do this via C# script as this script I believe needs to be running on the Board GameObject since it's the parent object rather than the tiles as children, although I could very easily be wrong. Thank you for any references to where you came up with your answers in advance, and if I missed an answer that could have been found easily via search I do apologize as I did attempt to search before asking.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Jun 11, 2013 at 09:01 PM

Without your source, it is difficult to advice a specific solution. The basic idea is to have all the block deselect themselves before processing the new click. There are a number of possible mechanisms for doing this deselection:

Since you are using C#, you can use events and delegates. Here is a tutorial[1]:

http://www.youtube.com/watch?v=N2zdwKIsXJs

You can use BroadcastMessage(). Note the script that calls this method does not have to be on the parent. You can do something like:

 transform.parent.gameObject.BroadcastMessage();

Assuming a common parent for the tiles, you could walk the transform list:

    foreach (Transform child in transform) {
         child.gameObject.GetComponent< SomeScript >().Deselect();
    }

If you are using Raycasting() (instead of OnMouseDown()) to do the selecting and all the tiles are tagged:

 var argo = GameObject.FindObjectsWithTag("tile");
 for (int i = 0; i < argo.Length; i++) {
    argo[i].GetComponent< SomeScript >().Deselect();
 }

And I'm sure there are other ways.

Comment

People who like this

0 Show 3 · 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 Jadeon777 · Jun 12, 2013 at 06:37 PM 0
Share

Thank you for the proposed solution @robertbu, I haven't started attempting to use Raycasting as I understand what it does but have never actually implemented it yet and am not 100% sure on how so I may experiment with that later tonight. I will attempt your other suggestions as well and report back with my results.

A question though as to your comment about walking the transform list. If we know which tile has been selected by a click would it not be more efficient to somehow store this tiles ID as the active tile and some how simply de-activate it when a new tile is selected rather than traversing a list/array of all possible tiles?

Thanks again!

avatar image robertbu · Jun 12, 2013 at 07:24 PM 0
Share

If you are going to be using Raycasting() from the outside rather than using OnMouseDown(), storing and using a selected state should work fine. In general I try to push information down as far as possible, and hide it as much as possible. Anything that I can put into the buttons/tiles I would.

avatar image Jadeon777 · Jun 14, 2013 at 02:59 AM 0
Share

I accepted your answer as I feel for many facing these types of issues this is a reasonable solution. I haven't gotten around to implementing it myself yet but I will report back when I do. @robertbu.

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

14 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

Related Questions

Make a simple tree 1 Answer

"Center On Children" programmatically 1 Answer

How can I listen for a function being called from a parent class? 1 Answer

Rotate similar child objects around own origin 2 Answers

Instantiating as a child error 1 Answer


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