Search Unity

Question How to flip the scale of just a parent GameObject and not its children

Discussion in 'Scripting' started by NatiSFG, Mar 7, 2023.

  1. NatiSFG

    NatiSFG

    Joined:
    Jul 21, 2017
    Posts:
    24
    I'm creating a 2D platformer game and I have a Player gameObject in the hierarchy that has its sprite. When the player is facing one way and they press the opposite arrow, I'd like to flip the sprite but not the camera and groundCheck children gameObjects underneath.

    The code below doesn't work and it gives me a
    NullReferenceException: Object reference not set to an instance of an object
    error.

    Code (CSharp):
    1. private void Flip() {
    2.    if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) {
    3.        isFacingRight = !isFacingRight;
    4.        Vector3 localScale = transform.localScale;
    5.        localScale.x *= -1f;
    6.        transform.parent.localScale = localScale;
    7.    }
    8. }
    When I replace
    transform.parent.localScale = localScale
    to
    transform.localScale = localScale
    it works except it flips all of the gameObjects, including the camera.
     
    Last edited: Mar 7, 2023
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,929
    If you don't want objects to inherit the transforms of their parent... then they shouldn't be children.

    In most cases, cameras are not children of the objects they are following, they're the children of some invisible game object that is following its target via code.

    You haven't told us what line of code this is happening on, but assuming it's happening on line 6, that would mean the game object doesn't have a parent game object.

    But in short, make these objects siblings of the player, and make them follow what they need to via code. Thus you can flip the player's sprite with impunity.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    With the camera as a separate object, as spiney suggested, you can put a script on it with an update that makes its transform position x and y the same as the players. Then you can do whatever you want to the player without it effecting the camera. You can also keep the players graphics on or in a child object of the player so that you can do things to it without effecting the rest of the player object. If you're using a sprite renderer and you don't need to also make colliders flip, you can just toggle Flip X on the sprite renderer and that will change the direction it's facing.
     
    RadRedPanda likes this.
  4. NatiSFG

    NatiSFG

    Joined:
    Jul 21, 2017
    Posts:
    24
    Thank you both!!!

    @QuinnWinters Because of the nature of my 2D platformer, I was able to just use the flipX boolean on the Sprite Renderer. I didn't notice it was there! This code works great:

    Code (CSharp):
    1.     private void Flip() {
    2.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) {
    3.             isFacingRight = !isFacingRight;
    4.             GetComponent<SpriteRenderer>().flipX = !isFacingRight;
    5.         }
    6.     }
     
    Last edited: Mar 7, 2023
  5. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Glad to hear you got it worked out. If you're going to toggle flip x every time the player changes direction, I suggest caching the sprite renderer component so you're not having to call GetComponent on it every time.