using UnityEngine;
    using System.Collections;
     
    public class ChangeEquip : MonoBehaviour {
     
        [SerializeField]
        private GameObject[] weapons;
        private int equipment;
        private Player player;
        private SpriteRenderer sprite;
        private int equipment_old;
    
        // Use this for initialization
        void Start() 
        {
            GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
            player = playerObj.GetComponentInParent < Player>();     
            sprite = GetComponent<SpriteRenderer>();
    
            //初期装備設定
            equipment = -1;
            equipment_old = equipment;
        }
     
        // Update is called once per frame
        void Update() {
            equipment = player.equip;
            if( equipment != equipment_old){
                if ( player.equip > -1){
                    InstantiateWeapon();
                }else{
                    DestroyWeapon();
                }
                equipment_old = equipment;
            }
        }       
        
        void InstantiateWeapon()
        {
            //素手ではない時だけ武器をインスタンス化
            if (equipment != -1) {
                //新しく装備する武器をインスタンス化
                var weaponObj = Instantiate<GameObject>(weapons[equipment]);
                var weapon = weaponObj.GetComponent<owrd>();
                //表示はスプライトに任せるためオブジェクトは見えないようにする
                weaponObj.transform.localScale = new Vector3( 0f, 0f, 0f);
                //スプライトに装備の画像を当てる
                sprite.sprite = weapon.GetComponent<SpriteRenderer>().sprite;
                sprite.transform.localPosition = weapon.attachPosition;
                //プレイヤーに今の武器番号を伝える
                player.equip = equipment;
            }
        }
    
        void DestroyWeapon(){
            equipment = -1;
            Destroy(transform.GetChild(0).gameObject);
            sprite.sprite = null;
            equipment_old = equipment;
        }
    }