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
1
Question by QuestionAsker · Aug 05, 2013 at 02:09 AM · gameobjectparentchildchildrenheirarchy

Target child of child without knowing name?

I need to target a GameObject, that is a child of an object without knowing said childs name.

My hierarchy is as follows:

Player/playerCamera/GunSlot1/"""variable prefab"""

In my code I need to select a string in a code in the great grandchild of player, without knowing the name of the great grandchild """variable prefab"""". In """variable prefab""" there will be a code attached to it. There will be a property (string name = "name or ID";). That's what I need selected.

I cant hardcode anything because the child of GunSlot1 will vary drastically.

i imagine the code would look like:

string gunName = Player.playerCamera.GunSlot1."""I dont know how to target a variable here""".scriptName.nameOrID;

Please ask if you have any questions, and thanks in advance!

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

Answer by aldonaletto · Aug 05, 2013 at 02:36 AM

A simple solution is to create a ID script that basically contains the object ID and attach it to all children - like this:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectId : MonoBehaviour {
     public string myId = "";
 }

Add this script to every child you want to identify, and set their IDs in the Inspector. You can then get all scripts of this type with GetComponentsInChildren and search for the one you want with a function like this (in a script attached to the parent):

 // return the transform of the desired object, or null:

 Transform FindById(string targetId){
     // get all ObjectId script down the hierarchy:
     Component[] children = GetComponentsInChildren<ObjectId>();
     // search for the desired ID:
     foreach (Component script in children){ 
         if ((script as ObjectId).myId == targetId){
             return script.transform; // if found, return its Transform
         }
     }
     return null; // if not found, return null
 }

EDITED: The old and good FPS Tutorial handled this by keeping only one of the weapons active at any moment and calling its functions by name via BroadcastMessage (SendMessage to any children). In order to shoot, for instance, it called the function Fire, which could be totally different in different weapons:

 if (Input.GetButtonDown("Fire1")){
   BroadcastMessage("Fire");
 }

The same approach may be used to get info about the weapon: BroadcastMessage accepts passing one argument to the target function, which may be a class reference - you can assign the desired info to the variables in such class, like this:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponManager : MonoBehaviour {
     public class WeaponInfo {
         public string name;
         public string ammoType;
     }

     WeaponInfo info = new WeaponInfo();

     // call this function to set info fields with the current weapon info:
     void GetWeaponInfo(){
         BroadcastMessage("GetInfo", info);
     }
 }

And add the function GetInfo to all weapon scripts:

 void GetInfo(WeaponInfo info){
     info.name = "M9";
     info.name = "9 mm";
 }

When you call GetWeaponInfo(), the fields of the variable info are set to the values defined in the current weapon.

WARNING: I usually write in JS, thus the C# code above may have some stupid errors!

Comment
Add comment · 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 QuestionAsker · Aug 05, 2013 at 02:51 AM 0
Share

I see. Thank you. This does work however, what if I don't know what ID I'm looking for? Is there any way to do that? Because ideally i would want a player to have a gun in the slot without knowing exactly what gun I need a script to find out which gun is there, so i can feed the guns properties to a shooting script.

avatar image aldonaletto · Aug 05, 2013 at 03:58 AM 0
Share

A good solution is to use Broadcast$$anonymous$$essage to call functions by name in the weapon scripts. Take a look at my answer - I edited it in order to include this approach.

avatar image QuestionAsker · Aug 05, 2013 at 01:03 PM 0
Share

This is exactly what I needed! Thank you so much!Im new to unity but old to coding so hopefully ill work out any errors. Thanks again this helps so much

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

15 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

Related Questions

Make a simple tree 1 Answer

How to check parent value instead name value(more details in post) 3 Answers

How to Access Components in Children of GameObject? 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Set up rank children 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