7cec8177abc236a14e81d2b628f17765b8f105


7fef8274abc236a14e81d2b628f1766d6f164d

Square의 하위개체인 weapon의 SpriteRenderer에 엑세스하려고 하는데 테스트하려고 플레이 누르기만 하면


7eee8375abc236a14e81d2b628f1776feb7794

이렇게 자기멋대로 개체가 변경되는데 어떻게 해결해야 하나요?


코드:


using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel.Design;

using Unity.VisualScripting;

using UnityEngine;


public class PlayerMove : MonoBehaviour

{


    public GameManager gameManager;

    public GameObject weapon;

    public SpriteRenderer childspriteRenderer;

    public float maxSpeed;

    public float jumpPower;

    Rigidbody2D rigid;

    SpriteRenderer spriteRenderer;

    Animator anim;


    [SerializeField] private LayerMask jumpableGround;

    private BoxCollider2D coll;

    private Vector2 boxCastSize = new Vector2(0.5f, 1.0f);


    // Start is called before the first frame update

    void Start()

    {

        rigid = GetComponent<Rigidbody2D>();

        spriteRenderer = GetComponent<SpriteRenderer>();

        childspriteRenderer = GetComponentInChildren<SpriteRenderer>();

        anim = GetComponent<Animator>();

        coll = GetComponent<BoxCollider2D>();


    }


    // Update is called once per frame

    private void Update()

    {

        


        if (Input.GetKey(KeyCode.LeftShift))

        {

            if (IsOnGround())

            {

                rigid.velocity = new Vector2(rigid.velocity.x, jumpPower);

                anim.SetBool("isJumping", true);

                

            }         

        }

        else if (IsOnGround() == false)

        {

            anim.SetBool("isJumping", false);

            

        }


        if (Input.GetButtonUp("Horizontal"))

            rigid.velocity = new Vector2(rigid.velocity.normalized.x * 0.5f, rigid.velocity.y);


        if (Input.GetButton("Horizontal"))

        {

            spriteRenderer.flipX = Input.GetAxisRaw("Horizontal") == -1;

            weapon.transform.position = new Vector2(transform.position.x - 0.6560f, weapon.transform.position.y);

            childspriteRenderer.flipX = true;  //하위개체의 spriteRenderer flipX 변경

        }


        if (rigid.velocity.normalized.x == 0)

            anim.SetBool("isWalking", false);

        else

            anim.SetBool("isWalking", true);




    }

    void FixedUpdate()

    { 

        float h = Input.GetAxisRaw("Horizontal");


        rigid.AddForce(Vector2.right * h, ForceMode2D.Impulse);


        if (rigid.velocity.x > maxSpeed)

            rigid.velocity = new Vector2(maxSpeed, rigid.velocity.y);

        if (rigid.velocity.x < maxSpeed*(-1))

            rigid.velocity = new Vector2(maxSpeed*(-1), rigid.velocity.y);


        Debug.Log(coll.bounds.size);

    }


    private bool IsOnGround()

    {

        return Physics2D.BoxCast(coll.bounds.center, boxCastSize, 0f, Vector2.down, .1f, jumpableGround);

    }

}