Multiple Melee Weapons

Haha, I could barely log into this place!

Ok so I’m attempting to make an RPG-like game. I want to have multiple weapons, inventory, stuff like that, but so far I’m only on the weapon part. I’ve been able to get a spear working with animations an a heirarchy like this:
FPS Player->
Main Camera->
Weapon Holder->
Weapon Mesh

I’ve animated the Weapon Holder, and that’s worked good, but now I need to learn how to switch weapons and I don’t know what to do next. I was going to try to destroy the Weapon Mesh when I press a button, and then instantiate it on the weapon holder(I’ve made a weapon template in Blender so all the meshes are zeroed in on the same spot to preserve the animations) but I’ve had trouble with instantiating in C# and can’t quite wrap my head around it.

I have my script on the Weapon holder, but need to destroy the child object and then instantiate a weapon prefab. Also my script wouldn’t be helpful, all I have is GetButton functions for the animations.

I want to learn this stuff so if you could post an example, and explain it to me it would be really appreciated.

On a side note, for the combat I was thinking of using trigger collisions, but it damages enemies even if you walk into them. If you could also help me understand raycasting a bit for that then it’d be helpful, if not it would just be a matter of instantiating an object with a box collider when i hit the attack button and destroying it after.

This script creates an array called weapons in the Inspector. Set the array size and drag your weapon prefabs to each empty slot. Create an empty object at the weapon position, assign this script to it and child it to Weapon Holder.

var weapons: GameObject[];
private var curWeapon: GameObject;

function ChangeWeapon(weapon:int){

  if (curWeapon){
  curWeapon = Instantiate(weapons[curWeapon],transform.position,transform.rotation);

function Update(){

  if (Input.GetKeyDown("1")) ChangeWeapon(0); 
  if (Input.GetKeyDown("2")) ChangeWeapon(1);
  if (Input.GetKeyDown("3")) ChangeWeapon(2);

You must adjust position and rotation of the empty object because the weapons will copy these characteristics when instantiated. Any time you call ChangeWeapon, the current weapon instance will be destroyed and the new one will be instantiated in its place.

About the attacks: you can add a trigger and a different tag to each weapon prefab, and in the enemies script use something like this:

function OnTriggerEnter(c:Collider){

  if (c.tag == "axe"){
  if (c.tag == "hammer"){

Of course, ApplyDamage is a function you must create, which will reduce enemy health and do whatever else you want when the enemy is hit (splash blood, scream etc.).
I’ve not tested these scripts, so if you have some trouble feel free to ask for help here! (in the comments, please)

InvalidCastException: Cannot cast from source type to destination type.

ive discovered a problem with this code! not sure why!