Switching GameObject

I’m trying to make it so when a player collides with an icon for a weapon that weapon is activated on the player. I have an array that holds the game objects that are the actual weapons and the icons and everything seems to activate except for getting the game objects to move to the currentWeapon. The debug logs are giving me all the information I need, just don’t know how to get the weapon working on the player. The error I get from Unity is, “Assets/Scripts/Player/SecondWeapon.cs(104,77): error CS0039: Cannot convert type SecondWeapon.WeaponPresetType' to UnityEngine.GameObject’ via a built-in conversion`” which is “GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;”

SecondWeapon.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SecondWeapon : MonoBehaviour {

	public GameObject[] weapons; //array of secondary weapons
	public GameObject currentWeapon; //current secondary weapon
	public int weaponFireRate;

	public Transform gunPoint;
		
	float nextFire;

	// Holds the weapons in our inventory
	private bool[] weaponInventory;
	// Which weapon is the player currently using?
	private int currentWeaponIndex = 0;

	public enum WeaponPresetType {
		
		Empty,
		Missle,
		Boomerang,
	}
	
	private WeaponPresetType _weaponPreset;
	
	public delegate void WeaponPresetChangeHandler();
	
	public event WeaponPresetChangeHandler WeaponPresetChanged;
	
	public WeaponPresetType weaponPreset {
		
		get {
			return _weaponPreset;
		}
		set {
			if (weaponPreset == value) return;
			
			gunPoint.transform.localPosition = new Vector2 (0.6f, 0f);
			
			_weaponPreset = value;
			if (WeaponPresetChanged != null) WeaponPresetChanged();
		}
	}
	
	void Start() {
		
		WeaponPresetChanged += WeaponPresetChangedHandler;

		weaponInventory = new bool[4];
		weaponInventory[(int)WeaponPresetType.Empty] = true; // Only set first weapon to true (put it in the inventory)
	}
	
	private void WeaponPresetChangedHandler() {

		switch (weaponPreset) {
			
		case WeaponPresetType.Empty:
			weaponInventory[(int)WeaponPresetType.Empty] = true;
			break;
		
		case WeaponPresetType.Missle:
			weaponInventory[(int)WeaponPresetType.Missle] = true;
			PickupWeapon(1);
			Debug.Log ("missle");
			break;
		
		case WeaponPresetType.Boomerang:
			//weaponInventory[(int)WeaponPresetType.Boomerang] = true;
			PickupWeapon(2);
			Debug.Log ("boomerang");
			break;

		default:
			weaponInventory[(int)WeaponPresetType.Empty] = true;
			break;
		}
	}
	
	void Update() {
		
		//sets B button as secondary fire
		if (Input.GetButton ("Fire2") && Time.time > nextFire) {
			nextFire = Time.time + weaponFireRate;
			Instantiate (currentWeapon, transform.position, transform.rotation) as GameObject;
		}
	}

	public void PickupWeapon(int weapon)
	{
		weaponInventory[weapon] = true;
		SwitchToWeapon(weapon);
		currentWeaponIndex = weapon;
		print("Current weapon pick up is: " + currentWeaponIndex);
	}

	public void SwitchToWeapon(int weapon)
	{
		if (weaponInventory[weapon])
		{
			// animate putting currentWeapon away
			GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;


			Debug.Log ("switched weapon is " + weapon);
			Debug.Log (weaponInventory[weapon]);
			// animate pulling out currentWeapon
		}
	}
}

From what I understood in your script … weapon is an int… so :

(WeaponPresetType)weapon

this is valid, and the result would be an enumeration based on your int index… but:

GameObject currentWeapon = (WeaponPresetType)weapon as GameObject;

This is not valid since, unity does not understand an enumeration as game object. Maybe you may need to refer to your weapon array of objects?

GameObject currentWeapon = weapons[weapon]; // since this is game object

Be awared that you already have a currentWeapon as global variable, so this local variable may be ghosting the global one, not sure if that’s something you want. Another option may be, keep your weapon and your equipped status within the same class something like:

public class WeaponObject {

public GameObject MyGameObject;
public bool IsEquipped;

}

That way you could keep the status along with the object, without worrying to synchronize 2 different arrays.

var WeaponObject weapon = new WeaponObject ();
var x = weapon.MyWeaponObject; // the weapon game object
var y = weapon.IsEquipped; // the equipped status for that specific weapon

Hope this helps you a bit.