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;
void Start()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
player = playerObj.GetComponentInParent < Player>();
sprite = GetComponent<SpriteRenderer>();
equipment = -1;
equipment_old = equipment;
}
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;
}
}