- Home /
Question by
Skorpion08 · Mar 08, 2021 at 10:04 PM ·
spriterenderer
How to flip the sprite together with all of the children,How to flip sprite with all of his children
I have an empty game object that has children but when I GetComponentInChildren(); it gets component only from the first child, how to make it to flip the sprite together with all of the children. Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteFlipper : MonoBehaviour
{
SpriteRenderer sr;
private void Awake()
{
sr = GetComponentInChildren<SpriteRenderer>();
}
private void Update()
{
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
{
if (sr != null)
{
sr.flipX = true;
}
}
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
{
sr.flipX = false;
}
}
}
,I have an empty game object that has children but when I GetComponentInChildren(); it gets component only from the first child how to make it to flip the sprite together with all of the children Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteFlipper : MonoBehaviour
{
SpriteRenderer sr;
private void Awake()
{
sr = GetComponentInChildren<SpriteRenderer>();
}
private void Update()
{
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
{
if (sr != null)
{
sr.flipX = true;
}
}
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
{
sr.flipX = false;
}
}
}
Comment
Best Answer
Answer by DL_se · Mar 08, 2021 at 10:23 PM
use SpriteRenderer[] sr; sr = GetComponentsInChildren(); iterate through them