# Can't Get .enabled to Work for Children Sprites. What Gives?

So I have an object with a few children. I want to be able to disable/enable 2 of their SpriteRenderers via one of my scripts. I feel like I'm doing things right, but apparently I'm not. And yes, my game objects have the appropriate tags that are being searched for in my code. I'll paste the relevant bits of code below:

public bool isAiming = false;
Animator playerAnim;

GameObject arm;
GameObject weapon;

SpriteRenderer armRenderer;
SpriteRenderer weaponRenderer;

void Start()
{
playerAnim = gameObject.GetComponent<Animator>();

arm = GameObject.FindGameObjectWithTag("Arm");
weapon = GameObject.FindGameObjectWithTag("EquippedWeapon");

armRenderer = arm.GetComponent<SpriteRenderer>();
weaponRenderer = weapon.GetComponent<SpriteRenderer>();
}

void Update()
{
CheckIfAiming();
}

void CheckIfAiming()
{
if (Input.GetButton("Fire2")) // Holding right click
{
isAiming = true;

armRenderer.enabled = true;
weaponRenderer.enabled = true;

playerAnim.SetBool("isAiming", isAiming);
}
else
{
isAiming = false;

armRenderer.enabled = false;
weaponRenderer.enabled = false;

playerAnim.SetBool("isAiming", isAiming);
}
}


What gives? I want to just enable the sprite rendering when I hold down the right mouse click. SetActive(false/true) is not an option as I still want code to affect them while they're not visible.

Am I just trying to access the SpriteRenderers incorrectly?

• Have a look at docs.unity3d.com/ScriptReference/… if the component you are trying to access is a child (in the scene hierarchy) of the object with this script. I think your hunch is correct you probably need to use GetComponentInChildren instead. – Pheonix2105 Jun 22 '18 at 1:39

Finally figured it out! Pheonix2105's answer wasn't quite what I was looking for, but it quickly led me to find the right answer.

Here's what I used:

Renderer[] renderChildren = GetComponentsInChildren<Renderer>();

int i;
for(i = 1; i < renderChildren.Length; ++i)
{
renderChildren[i].renderer.enabled = true;
}


And then when I wanted the SpriteRenderers to be disabled I did the same thing, except I set it to false. I started i equal to 1 so that it would skip the parent object's renderer and only change the children.