- Home /
How do you enable/disable a child object individually? C#
I want to be able to enable/disable child objects individually using (possibly) gameObject.SetActive(false); and gameObject.SetActive(true); in Unity 4. I want to be able to cycle through a couple of game objects that are children of a larger object. The goal is to have a spaceship with multiple guns that are in one spot on the ship. Pressing tab (for example) would cycle through, say, three different weapons. Is there a simple way to do this? Each gun would have its own particle effects, physics, animations and sounds that would need to be disabled/enabled as well. Thanks in advance.
var changeWeapon : boolean = true;
var weapon1 : GameObject;
var weapon2 : GameObject;
function Update()
{
changeWeapon = !changeWeapon;
if(Input.Get$$anonymous$$eyDown("tab"))
{
if(changeWeapon){
weapon1.SetActive(true);
weapon2.SetActive(false);
} else {
weapon1.SetActive(false);
weapon2.SetActive(true);
}
}
}
Answer by orb · Sep 08, 2014 at 06:43 AM
I think the answer is a little more advanced than simply setting all objects in a hierarchy inactive. When you use the SetActive() method you also toggle the visibility of the game object - it doesn't seem right to me having guns disappear!
Here's how I'd do it:
One game object on the ship needs to act as the parent of the guns
Each gun object has a component class, for example ShipGun
Each ShipGun has a boolean that holds whether it can be fired and a pointer to an "on" state particle effect attached to it
Finally each ShipGun should have its own on/off method(s) which toggle "canFire" (or whatever) and use SetActive(canFire) on its particle effect
You should get the list of guns with GetComponentsInChildren() and refresh it only if the guns are changed. Then you can rotate the active one by walking through this list. Pseudo-code:
ShipGun[] guns = gunMount.GetComponentsInChildren<ShipGun>();
int activeGun = 1;
for(int i=0;i<guns.Length;i++)
{
if(i == activeGun)
{
guns[i].ToggleState(true);
guns[i].canFire = true;
} else {
guns[i].ToggleState(false);
guns[i].canFire = false;
}
}
$$anonymous$$aking the Guns disappear is exactly what i want. The objective(maybe I wasn't clear, I apologize)is to be able to switch what weapons are attached (and active) to the ship before a battle. Im very new to Unity, so bear with me.To sum it up, by pressing the left and right arrows, you switch between three weapons (wep1, wep2, wep3 for example). Each of these three weapons attaches to the ship in the same spot, so you can only equip and activate one at a time. How would that be coded?
(I posted a reply earlier, but it seems to be missing in action!)
$$anonymous$$ake an integer variable activeGun, and increase it with each press of TAB. Set it to zero if it goes past the max number of weapons. Also make a public variable for the particle effect on each gun and stick it there.
Now the loop should contain something like this:
if(i == activeGun)
{
guns[i].SetActive(true);
guns[i].particle.SetActive(true);
} else {
guns[i].SetActive(false);
guns[i].particle.SetActive(false);
}