- Home /
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!
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!
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.
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.
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