Search Unity

Loadout System Help

Discussion in 'Scripting' started by NaviLlicious, Jun 13, 2015.

  1. NaviLlicious

    NaviLlicious

    Joined:
    Jun 13, 2015
    Posts:
    3
    Hello everyone, this may have been asked a lot but I can't find anything that would help me on this. I am trying to make a FPS game and have gotten decently far on the programming bit but have gotten stuck on this for a few days. I have 3 different scripts that allow me to select which weapon the player wants to spawn with before the game starts but it only allows you to select these weapons when the match starts and it has to be limited to reduce screen clutter. What I'm wanting to do is to move the weapon selection to the main menu under a sub menu I have created in the script, similar to Call of Duties "Create-A-Class" system, but have no idea how to do that. Here is my scripts so you can see what I have:

    GUIManager:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class GUIManager : MonoBehaviour {
    public List<string> WeaponNames = new List<string>();
    public List<string> SightNames = new List<string>();
    public int CurWeapon;
    public int LastWeapon;
    public static GUIManager Instance;
    public bool ShowBoard;
    public WeaponManager manager;
    public RankManager rank;
    // Use this for

    void Start () {
    Instance = this;
    // foreach (Sight s in NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[0].Sights.ToArray())
    // {
    // SightNames.Add(s.Name);
    // }
    }

    // Update is called once per frame
    void Update () {
    if(LastWeapon != CurWeapon)
    {
    ChangedGun();
    }

    UpdateWeapons();

    //ShowBoard = Input.GetKey(KeyCode.Tab);

    if (Input.GetKeyDown(KeyCode.Tab))
    ShowBoard = !ShowBoard;
    }

    void OnGUI()
    {
    if (NetworkManager.Instance.MatchStarted && !NetworkManager.Instance.MyPlayer.IsAlive)
    {
    CurWeapon = GUILayout.SelectionGrid(CurWeapon, WeaponNames.ToArray(), 4);
    NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].CurSight = GUILayout.SelectionGrid(NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].CurSight, SightNames.ToArray(), 4);
    if (!NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].Bought)
    {
    if (NetworkManager.Instance.MyPlayer.Score >= NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].Cost)
    {
    GUI.BeginGroup(new Rect(Screen.width / 2 - 256, Screen.height / 2 - 128, 512, 256),"", "box");
    GUI.Label(new Rect(128, 16, 256, 32), "Would you like to buy this weapon?");
    if (GUI.Button(new Rect(128, 64, 256, 32), "Confirm"))
    {
    NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].Bought = true;
    NetworkManager.Instance.MyPlayer.Score -= NetworkManager.Instance.MyPlayer.Manager.FirstPersonCont.Weapons[CurWeapon].Cost;
    }
    }
    else
    {
    GUI.BeginGroup(new Rect(Screen.width / 2 - 256, Screen.height / 2 - 128, 512, 256),"", "box");
    GUI.Label(new Rect(128, 16, 256, 32), "You do not have sufficient funds!");
    }
    GUI.EndGroup();
    }
    }

    if (ShowBoard && NetworkManager.Instance.MatchStarted)
    {
    GUILayout.BeginArea(new Rect(Screen.width/4, Screen.height/4, (Screen.width) - (Screen.width/2), (Screen.height) - (Screen.height/2)),GUIContent.none,"box");
    foreach(Player pl in NetworkManager.Instance.PlayerList)
    {
    GUILayout.BeginHorizontal();
    GUILayout.Label(pl.PlayerName);
    GUILayout.Label(pl.Kills.ToString());
    GUILayout.Label(pl.Deaths.ToString());
    GUILayout.Label(pl.Score.ToString());
    GUILayout.EndHorizontal();
    }
    GUILayout.EndArea();
    }

    if (NetworkManager.Instance.MyPlayer.IsAlive)
    {
    GUI.Box(new Rect(Screen.width - (128 * (NetworkManager.Instance.MyPlayer.Health / 100)), 16, (NetworkManager.Instance.MyPlayer.Health / 100) * 256, 32), NetworkManager.Instance.MyPlayer.Health.ToString());
    }
    }

    public void UpdateWeapons()
    {
    SightNames.Clear();
    WeaponNames.Clear();
    foreach (Gun g in manager.Weapons)
    {
    if (rank.CurLevel >= g.UnlockLevel)
    {
    WeaponNames.Add(g.Name);

    }
    }

    ChangedGun();
    }

    void ChangedGun()
    {
    SightNames.Clear();
    LastWeapon = CurWeapon;
    foreach (Sight s in manager.Weapons[CurWeapon].Sights)
    {
    if (manager.Weapons[0].Kills >= s.UnlockKills)
    {
    SightNames.Add(s.Name);
    }
    }
    }
    }

    Weapon Manager:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class WeaponManager : MonoBehaviour {
    public List<Gun> Weapons = new List<Gun>();
    public static WeaponManager 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;
    transform.root.GetComponent<UserPlayer>().Server_GetGun(Weapons[CurWeapon].Name);
    ApplyWeapon();
    }

    public void ApplyWeapon()
    {
    foreach (Gun gu in Weapons)
    {
    if(gu == Weapons[CurWeapon])
    {
    gu.gameObject.SetActive(true);
    }
    else
    {
    gu.gameObject.SetActive(false);
    }
    }
    }

    public static Gun FindWeapon(string Name)
    {
    foreach(Gun Gu in Instance.Weapons)
    {
    if (Name == Gu.Name)
    return Gu;
    }
    return null;
    }
    }

    And the Menu script:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class Menu : MonoBehaviour {

    public Menu instance;
    private string CurMenu;
    public string Name;
    public string MatchName;
    public int Players;
    // Use this for initialization
    void Awake () {
    CurMenu = "Main";
    instance = this;
    //Name = PlayerPrefs.GetString ("PlayerName");
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update () {

    }

    public void ToMenu(string menu){
    CurMenu = menu;
    }

    void OnGUI(){
    if(CurMenu == "Main")
    Main();
    if (CurMenu == "Host")
    Host ();
    if (CurMenu == "Lobby")
    Lobby ();
    if (CurMenu == "List")
    MatchList();
    if (CurMenu == "Level")
    Levels();
    if (CurMenu == "CreateClass")
    CAC();
    if (CurMenu == "Auto")
    AutoRifles();
    if (CurMenu == "SubMG")
    CACSubMG();

    }

    private void Main(){
    if (GUI.Button(new Rect(Screen.width/2 - 60, Screen.height/2 - 75, 128, 32), "Host Game")) {
    ToMenu ("Host");
    }

    if (GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2, 128, 32), "Join Game"))
    {
    ToMenu("List");
    }

    GUI.Label(new Rect(Screen.width - 128, Screen.height - 32, 128, 32), "Alpha 0.12");

    }

    private void Host(){

    if (GUI.Button (new Rect(Screen.width / 2 + 116, Screen.height / 2 + 32, 128, 32), "Enter Lobby")) {
    NetworkManager.Instance.StartServer(MatchName, Players);
    ToMenu ("Lobby");
    }

    MatchName = GUI.TextField (new Rect(Screen.width / 2 - 65, Screen.height / 2 - 95, 128, 32), MatchName);
    GUI.Label (new Rect(Screen.width / 2 - 40, Screen.height / 2 - 115, 128, 32), "Match Name");
    Players = Mathf.Clamp(Players, 1, 18);
    GUI.Label (new Rect(Screen.width / 2 - 40, Screen.height / 2 - 40, 128, 32), "Max Players");
    if(GUI.Button (new Rect(Screen.width / 2 + 40, Screen.height / 2 - 15, 128, 32), "+"))
    Players ++;
    GUI.Label (new Rect(Screen.width / 2 - 10, Screen.height / 2 - 20, 128, 32), Players.ToString());
    if(GUI.Button (new Rect(Screen.width / 2 - 175, Screen.height / 2 - 15, 128, 32), "-"))
    Players --;

    if (GUI.Button (new Rect(Screen.width / 2 - 245, Screen.height / 2 + 32, 128, 32), "Back")) {
    ToMenu ("Main");
    }
    GUI.Label(new Rect(Screen.width - 128, Screen.height - 32, 128, 32), "Alpha 0.12");
    }

    private void Lobby()
    {

    if (Network.isServer)
    {

    if (GUI.Button(new Rect(Screen.width - 1250, Screen.height - 500, 128, 32), "Begin Match"))
    {
    NetworkManager.Instance.networkView.RPC("LoadLevel", RPCMode.All, NetworkManager.Instance.CurLevel.LoadName);
    //networkView.RPC("StartServer", RPCMode.All);
    }

    }

    if (GUI.Button(new Rect(Screen.width - 1250, Screen.height - 450, 128, 32), "Select Map"))
    {
    ToMenu("Level");
    }

    if (GUI.Button(new Rect(Screen.width - 1250, Screen.height - 375, 128, 32), "Back"))
    {
    ToMenu("Main");
    Network.Disconnect();
    }

    GUILayout.BeginArea(new Rect(Screen.width - 500, Screen.height - 500, 128, 32));
    foreach (Player pl in NetworkManager.Instance.PlayerList)
    {
    //if (pl == NetworkManager.Instance.MyPlayer)
    //{
    // GUI.color = Color.yellow;
    // }

    GUI.color = pl.CurTeam.TeamCol;

    GUILayout.Label(pl.PlayerName);
    GUI.color = Color.white;
    }

    GUILayout.EndArea();
    GUI.Label(new Rect(Screen.width - 128, Screen.height - 32, 128, 32), "Alpha 0.12");
    }

    private void MatchList()
    {
    if (GUI.Button(new Rect(Screen.width/2 - 590, Screen.height - 100, 128, 32), "Refresh"))
    {
    MasterServer.RequestHostList("Project Onyx");
    }

    if (GUI.Button(new Rect(Screen.width / 2 - 590, Screen.height - 43, 128, 32), "Back"))
    {
    ToMenu("Main");
    }

    GUILayout.BeginArea(new Rect(Screen.width / 2 - 340, 0, Screen.width / 2, Screen.height), "Server List","box");
    foreach (HostData hd in MasterServer.PollHostList())
    {
    GUILayout.BeginHorizontal();
    GUILayout.Label(hd.gameName);
    if (GUILayout.Button("Connect"))
    {
    Network.Connect(hd);
    ToMenu("Lobby");
    }

    GUILayout.EndHorizontal();
    }
    GUILayout.EndArea();
    GUI.Label(new Rect(Screen.width - 128, Screen.height - 32, 128, 32), "Alpha 0.12");
    }

    private void Levels()
    {
    foreach(Level lvl in NetworkManager.Instance.ListOfLevels)
    {
    if (GUILayout.Button(lvl.PlayName))
    NetworkManager.Instance.CurLevel = lvl;
    }

    if (GUILayout.Button("Back"))
    {
    ToMenu("Lobby");
    }

    GUI.Label(new Rect(Screen.width - 128, Screen.height - 32, 128, 32), "Alpha 0.12");
    }

    public void CAC()
    {

    }

    public void AutoRifles()
    {

    }

    public void CACSubMG()
    {

    }
    }

    I'm not asking for anyone to do this for me, what I am asking is if anyone can help me or put me in the right direction with the scripts I already have, although if someone sent me a C# script for it with directions I wouldn't complain ;). Like I said the player can only select the weapon when the game starts but I want to make it so they can select the guns they want through the main menu within a sub menu I have created, if you looked through the menu script you will see empty menu's called "CAC", "AutoRifles" and "CACSubMG", these are test menu's that I have been toying around with but have not had any luck yet :(. If anyone can help I would really appreciate it, I have been banging my head on this for a while now lol. Oh, I forgot to add that these are in C#.
     
    Last edited: Jun 13, 2015
  2. legoer23

    legoer23

    Joined:
    Jan 5, 2018
    Posts:
    2
    It's not working in Unity 2017. What are the fixes for the errors i get?