Can't get FPS to switch weapons properly

I’ve been following the FPS tutorial and I keep ending up with issues.

My latest issue is that I can’t get the player to switch among the 3 weapons properly.

Am I supposed to modify the scripts in order to get this done properly?

I think theres only two weapons yea?

Hallo,

maybe that helps you:

the script should be added to the weapon-gameobject where all weapons are in

function Start () {
	// Select the first weapon
	SelectWeapon(0);
}


function Update () {
	// Did the user press fire?
	if (Input.GetButton ("Fire1"))
		BroadcastMessage("Fire");
	
	if (Input.GetKeyDown("1"))
	{
		SelectWeapon(0);
	}	
	else if (Input.GetKeyDown("2"))
	{
		SelectWeapon(1);
	}	
    else if (Input.GetKeyDown("3"))
	{
		SelectWeapon(2);
	}	
	else if (Input.GetKeyDown("4"))
	{
		SelectWeapon(3);
	}	


}

function SelectWeapon (index : int) {
	for (var i=0;i<transform.childCount;i++)
	{
		// Activate the selected weapon
		if (i == index)
			transform.GetChild(i).gameObject.SetActiveRecursively(true);
		// Deactivate all other weapons
		else
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
	}
}

and thats how to position weapons in the hierachy:

180073--6420--$hierachy_144.png

Hey Achim,

Thanks! That resolved the problem, however it only works in the editor and it doesn’t work when I build and play the game.

These are the errors that I am getting

FAILED(hr)
UnityEditor.Handles:Internal_DrawCamera(Camera, Int32)
UnityEditor.Handles:Internal_DrawCamera(Camera, Int32)
UnityEditor.Handles:smile:rawCamera(Rect, Camera, Int32) (at C:\builds\unity-trunk\unity\Editor\Mono\Generated\EditorHandles.cs:731)
UnityEditor.SceneView:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\SceneView\SceneView.cs:350)
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[ ], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[ ])
UnityEditor.HostView:Invoke(String) (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:156)
UnityEditor.DockArea:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:491)

[…..\Runtime\GfxDevice\d3d\D3D9VBO.cpp line 504]

BroadcastMessage Fire has no receiver!
UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.Component:BroadcastMessage(String) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\BaseClass.cs:532)
PlayerWeapons:Update() (at Assets\WeaponScripts\PlayerWeapons.js:13)

[…..\Runtime\Mono\MonoExportUtility.cpp line 480]

FAILED(hr)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
UnityEditor.GameView:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\GameView\GameView.cs:192)
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:InternalInvoke(Object, Object[ ])
System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[ ], CultureInfo)
System.Reflection.MethodBase:Invoke(Object, Object[ ])
UnityEditor.HostView:Invoke(String) (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:156)
UnityEditor.DockArea:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:491)

[…..\Runtime\GfxDevice\d3d\D3D9VBO.cpp line 504]

Also, what would I have to do if I wanted to rearrange the the order of the weapons?

sry cant help you with the errors but to rearrange the order simply rename the objects like i showed in the 2nd picture.

a weapon 1
b weapon 2

or

a weapon 2
b weapon 1

Hey thanks Achim, you’ve been a great help!
I still can’t figure out why the game plays well in the editor and doesn’t when I build it though. I’m sure I’ll figure it out sooner or later though.

hey, ive been able to do the weapon switching and its working great!! but i have a new question - does anyone know how to switch between a weapon and audio?

ie/
press 1 for weapon
press 2 for play audio

thanks so much!!! ive tried a few things…
suyin

never mind! i just altered the code to this, and its been working great!

if (Input.GetKeyDown("2")) {
   audio.Play();

   }