- Home /
Weapon Selection Script
Hey, I need help with a feature I want to add to my game. I am trying to create a script that allows the player to choose the Weapon that he wants to spawn with. I have created two scripts that are supposed to do this and it works, but when it comes to selecting what weapon I want to use, the second grid on the selection grid cannot be selected for some reason. I have assigned all the weapons and I think it should work. If someone could help, it would be much appreciated! Thank you in advance.
This is the script that is used for the GUI that are supposed to pop up (Note: an external line of Code tells the script when to display the GUI but it works so I did not put it here)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GUIManager : MonoBehaviour
{
public List<string> WeaponNames = new List<string>();
public int CurWeapon;
public static GUIManager Instance;
// Use this for initialization
void Start ()
{
Instance = this;
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
if(NetworkManager.Instance.MatchStarted && !NetworkManager.Instance.MyPlayer.IsAlive)
{
GUILayout.SelectionGrid(CurWeapon, WeaponNames.ToArray(), 4);
}
}
}
The script that manages the Weapons and switches them
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WeaponsManager : MonoBehaviour
{
public List<Gun> Weapons = new List<Gun>();
public static WeaponsManager Instance;
public int CurWeapon;
// Use this for initialization
void Start ()
{
Instance = this;
}
// Update is called once per frame
void Update ()
{
}
public void Spawn ()
{
CurWeapon = GUIManager.Instance.CurWeapon;
ApplyWeapon ();
}
public static Gun FindWeapons (string Name)
{
foreach (Gun gu in Instance.Weapons)
{
if(Name == gu.Name)
return gu;
}
return null;
}
public void ApplyWeapon ()
{
foreach (Gun gu in Weapons)
{
if(gu == Weapons[CurWeapon])
{
gu.gameObject.SetActive(true);
}
else
{
gu.gameObject.SetActive(false);
}
}
}
}
throw some Debug.Log()'s that tell you what's doing on in each of your methods, that should help you debug and solve your problem.
Answer by Jamora · Aug 02, 2013 at 06:49 PM
SelectionGrid returns an int of the current selection which you need to store if you want to be able to change the selection.
You'll need
CurWeapon = GUILayout.SelectionGrid(CurWeapon, WeaponNames.ToArray(), 4);
Your answer
Follow this Question
Related Questions
Can anyone give an easy C# code to create a list of rooms in a multiplayer with Photon/PUN 0 Answers
fps multiplayer camera set (posible script) 1 Answer
change weapon only one player in network 0 Answers
non-authorative fps, client controlled character 0 Answers
FPS - Gun Delay while Rotating 0 Answers