Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 which i will hopefully find answers. If let's say i have two game objects with two children each, and both main and children 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 this,

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

As i understand, it is impossible doing this 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.GetChild(0) returns part 1 of object whose transform we are referring from this 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.GetChild(0) != null)){ //if you are getting something from button and the child exists
       transform.GetChild(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

Thank you for your answer, i understand what you mean there, but how would i access the objects transform dynamically (from collision lets say), because what if i have hundreds of these objects?

avatar image Owen-Reynolds · Jul 21, 2013 at 03:42 PM 1
Share

I prefer using transform.Find("part1") ins$$anonymous$$d of getChild(0), since the order of children isn't always the same as in the Inspector. But they both search only children, and can be mixed with GO Find. See the docs, plus exs:

GameObject.Find("spot").transform.Find("part1") or Transform p1 = collision.transform.Find("part1");

Legal to find something that doesn't exist, so could check if(p1!=null) from the previous example.

avatar image
1

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

As far as I understand your game objects are clones which 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 searching that object from your avatar? then you can set object trigger to fire event on colliding and check user input on object itself like this:

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

So instead of searching 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

Thank you for an answer. I was thinking to try using trigger event on each item, but is this performance "friendly" way? Because if i would have 100 objects, there would be 100 trigger checks each frame event if the player is very far away from it. Hmm, or maybe i could disable the objects trigger checking if player is 50 meters far away, but then again, there's a checking going on about the distance.

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.

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

Activate objects/crafting in a game. 1 Answer

Instantiated cloned GameObject returns Null outside Start method 0 Answers

Multiple Audio Source Problem 1 Answer

How do you access a script if you don't know it's name? 5 Answers

Accessing Variables from another object Script 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