Hi,
I’m trying to access the active child of a gameObject, I have two children on the gameObject that I switch between with key 1 and key 2, (I will be adding more) so all I want is for the var other to return the active child but I’m a bit confused how to do this I know the 0 returns the first child, but I want it to look through all the children of the gameObject and only return the active one?
Thanks
private var other : GameObject;
function Awake() {
other = GameObject.Find("A2Button"); // Find the gameObject A2Button
}
function Update () {
var other = gunCamera.transform.GetChild(0); // Set other to the current Weapon
Debug.Log(other);
}
3 Answers
3
This should search through the children of gunCamera and set ‘other’ to the active one.
private var other : GameObject;
function Update () {
if (gunCamera.transform.childCount > 0) {
for (var _c = 0; _c < gunCamera.transform.childCount; _c++) {
//Now check if the current child in the loop is active. If there is more than one active child it will cause issues.
if (gunCamera.transform.GetChild(_c).active) {
var other = gunCamera.transform.GetChild(_c); //Set other to the current active Weapon
Debug.Log("Found " + other.name + " at Vector3" + other.transform.localPosition + " in local space.");
}
}
}
}
Hi, SilverFoxMedia I’m trying to access a var off the child, I’ve added this line below the one in the script “var other”
var amo = gunCamera.transform.GetChild(_c).GetComponent("AnimationCtrlWeapon").bulletsRockets;
But I get error - ‘bulletsRockets’ is not a member of ‘UnityEngine.Component’.
bulletRockets is a int; var, Can someone please point me in the right direction.
Thank you.
private var other : GameObject;
function Update () {
if (gunCamera.transform.childCount > 0) {
for (var _c = 0; _c < gunCamera.transform.childCount; _c++) {
//Now check if the current child in the loop is active. If there is more than one active child it will cause issues.
if (gunCamera.transform.GetChild(_c).gameObject.active) {
var other = gunCamera.transform.GetChild(_c); //Set other to the current active Weapon
Debug.Log("Found " + other.name + " at Vector3" + other.transform.localPosition + " in local space."); //Send object name and position to debug log
}
}
}
}
Stupid me, I left the " " remarks around AnimationCtrlWeapon … It’s working now …
var amo = gunCamera.transform.GetChild(_c).GetComponent(AnimationCtrlWeapon).bulletsRockets;
Thank you very much, that's done it for me ..
– GriffoMe again, it does throw up a Warning message saying WARNING: 'UnityEngine.Component.active' is obsolete. the active property is deprecated on components. Please use gameObject.active instead. If you meant to enable / disable a single component use enabled instead. So I take that as Unity will be removing .active in the near future, It won't let me use enabled as 'enabled' is not a member of 'UnityEngine.Transform'. Is there another way to write that line to make it future proof? Thanks.
– GriffoOnce again thank you.
– Griffo