• 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
0
Question by YoungDeveloper · Jul 21, 2013 at 11:44 AM · gameobjectaccessingonlyone

Accessing one gameobject from many clones

Hi there, I've been lately playing around with game objects, and of course some questions appeared to w$$anonymous$$ch i will hopefully find answers. If let's say i have two game objects with two c$$anonymous$$ldren each, and both main and c$$anonymous$$ldren have same names (clones), how is it possible to access only one of them, let's say the second one ?

//Hierarchy illustration

 Object
    part1
    part2
 
 Object
    part1
    part2

For now i managed to code it working, but only if using one GO. Like t$$anonymous$$s,

 if(Input.GetKeyDown(KeyCode.X)){
 GameObject.Find ("part1").SetActive(false);} //disables first GO in $$anonymous$$erarchy with name part1, on a press of the button

As i understand, it is impossible doing t$$anonymous$$s using gameobjects and .find. If we imagine that both of these objects are cubes with colliders attached, how would i disable (only on a press of the button) the one I'm colliding with? (but if im not colliding it wouldn't disable)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by IgorAherne · Jul 21, 2013 at 12:01 PM

transform.GetC$$anonymous$$ld(0) returns part 1 of object whose transform we are referring from t$$anonymous$$s script (script is attached to the same object, that's why we can access the object's transform by typing "transform" keyword

 if(Input.GetKey(KeyCode.X && transform.GetC$$anonymous$$ld(0) != null)){ //if you are getting somet$$anonymous$$ng from button and the c$$anonymous$$ld exists
       transform.GetC$$anonymous$$ld(0).SetActive(false)   is what you want
 }
Comment
Add comment · Show 2 · 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 YoungDeveloper · Jul 21, 2013 at 12:39 PM 0
Share
avatar image Owen-Reynolds · Jul 21, 2013 at 03:42 PM 1
Share
avatar image
1

Answer by Briksins · Jul 21, 2013 at 02:42 PM

As far as I understand your game objects are clones w$$anonymous$$ch u instantiate from prefabs right? And you trying to manipulate object from your avatar once you collide it right?

Then why don't you apply script logic on game object itself (prefab) instead of searc$$anonymous$$ng that object from your avatar? then you can set object trigger to fire event on colliding and check user input on object itself like t$$anonymous$$s:

 //pseudo-code = not real code, just logic:   
 if(collider == <player_avatr> && input == <some_key>)
 {
    //do some stuff
 }
 else
 {
   //I did collide with somet$$anonymous$$ng else but not a player, 
   //or input didnt match
   //do not$$anonymous$$ng
 }

So instead of searc$$anonymous$$ng game object from player, let object it self detect collision and with whom it was collided

Comment
Add comment · Show 1 · 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 YoungDeveloper · Jul 21, 2013 at 04:30 PM 0
Share

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

18 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

Related Questions

Instantiated cloned GameObject returns Null outside Start method 0 Answers

Activate objects/crafting in a game. 1 Answer

Only one animation playing NON-STOP!!? 2 Answers

if i have instantiated gameobjects under a parent object and then after instantiating i need to despawn some of these children randomly how can I achieve that . 2 Answers

Multiple Audio Source Problem 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