How to make a map voting system

Updated on August 27, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
11 on August 16, 2017

I’m making my own fps multiplayer game and i need a map vote system. I’m currently making scenes(maps) that will be playable in the game, but the players themself can vote for what map they want to play. It displays the image of the map that is clickable to vote for and whichever with highest votes will be loaded. Can somebody help me with this. How do i make it?

  • Liked by
  • HansenDJ
Reply
8 on August 16, 2017

I’m not the best at using Unity because I’ve only been using it for only about 4 months but I google searched “map voting unity” and this was the 3rd result it may help. It has a script written out on it.

http://answers.unity3d.com/questions/676736/implementing-a-map-chooser-for-a-multiplayer-fps.html

on August 16, 2017

Thanks for your answer! I looked in to it but didn’t quite understand it all, i will probably have to make prefabs and setup many other things after i have added the script.. Right? If you know anything that i don’t, please help me out. Thank you!

on August 17, 2017

What have you done so far, and what are you stuck on right now? Like have you already made the map images that you can click on? Do you just need help on how to make it cast a vote when you click the picture and then how to make those votes do something like have the one with the most load up that scene? You might want to Google this because I’m not that experienced but I’ll help you as much as I can.

on August 17, 2017

An idea I have is to put the map chooser on it’s own scene where it is just a UI menu that has the map pictures be buttons and when you click it, it adds 1 to a variable. Have a variable for each map called like “Map01Votes”, “Map02Votes”, “Map03Votes”, “Map04Votes”, etc. and set each one equal to 0, then when it is clicked it adds one to that variable. And then compare each map to all the other ones and say “if (Map01Votes > Map01Votes && Map01Votes > Map03Votes && Map01Votes > Map04Votes)” then have it load the scene for map 1. Just do an if statement like this for each map. This might work, I’ll try and make something like this.

on August 17, 2017

Thanks man! I really appreciate your work and that you are willing to help me.
I haven’t done so much work with maps yet, i’m done with my main menu and i’m almost done with everything (in) game. I started making the first map yesterday.
You said that you will try to make something “like this” in your previous post, could you please get back to me with some information later on.
Thanks!

on August 17, 2017

I figured it out. So what I said above works. DM me your email address and I’ll send you my Unity Project files so you can check it out. I started by making going in the “Hierarchy” and clicking “Create” and then clicking “UI” and then “Image” and I just made it grey for my background and then I just made 4 buttons and made them children of the background and then put an image, set to “Sprite (2D and UI)” which is under “Texture Type” in the “Inspector” when you click on the .png or .jpg file, on each button. I then attached this script called “MapLoader” to the background :

 

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

 

public class MapLoader : MonoBehaviour {

 

     public float WesternPropsPackVotes = 0;
     public float DesertEnviroVotes = 0;
     public float CubethonVotes = 0;
     public float PlatformerVotes = 0;
     public Text WesternPropsPackNum;
     public Text DesertEnviroNum;
     public Text CubethonNum;
     public Text PlatformerNum;

 

     public void Update()
     {
          NumberOfVotes();
          MapChooser();
     }

     public void WesternPropsPackClick ()
     {
          WesternPropsPackVotes++;
     }

 

     public void DesertEnviroClick ()
     {
          DesertEnviroVotes++;
     }

 

     public void CubethonClick ()
     {
          CubethonVotes++;
     }

 

     public void PlatformerClick ()
     {
          PlatformerVotes++;
     }

 

     public void NumberOfVotes()
     {
          WesternPropsPackNum.text = WesternPropsPackVotes.ToString();
          DesertEnviroNum.text = DesertEnviroVotes.ToString();
          CubethonNum.text = CubethonVotes.ToString();
          PlatformerNum.text = PlatformerVotes.ToString();
     }

 

     public void MapChooser()
     {

          // Start WesternPropsPack Scene
    if (Time.time >= 10f && WesternPropsPackVotes > DesertEnviroVotes && WesternPropsPackVotes > CubethonVotes && WesternPropsPackVotes > PlatformerVotes)
          {
               SceneManager.LoadScene(“Box and Bottles Shooting”);
          }

 

          // Start DesertEnviro Scene
    if (Time.time >= 10f && DesertEnviroVotes > WesternPropsPackVotes && DesertEnviroVotes > CubethonVotes && DesertEnviroVotes > PlatformerVotes)
          {
               SceneManager.LoadScene(“Desert Enviro Example”);
          }

 

          // Start Cubethon Scene
   if (Time.time >= 10f && CubethonVotes > WesternPropsPackVotes && CubethonVotes > DesertEnviroVotes && CubethonVotes > PlatformerVotes)
          {
               SceneManager.LoadScene(“Menu”);
          }

 

          // Start 2D Platformer Scene
   if (Time.time >= 10f && PlatformerVotes > WesternPropsPackVotes && PlatformerVotes > DesertEnviroVotes && PlatformerVotes > CubethonVotes)
          {
               SceneManager.LoadScene(“MainLevel”);
          }

     }
}

 

You also have to create 4 text objects and parent them to the “Background”. You do this by clicking “Create” in the “Hierarchy” and then click “UI” and then “Text” and name each one “(the name of each map” and then “Votes”)” like how I named one of mine “DesertEnviroVotes”. Then add the “MapVoter” script to each of them and then there should be an empty slot under the “MapVoter” script attached, to put in a text object for each mapvotes (like how one of mine says “PlatformerVotes” under the “MapVoter” script since “PlatformerVotes” is the name of one of my Text variables) there you need to drag in each mapvotes text object from the “Hierarchy” to the “Text” slot under the script. You also need to add each mapvotes text object to the “MapVoter” script attached to the “Background”.

 

To make the game more efficient, instead of dragging in all of the mapvotes text objects from the “Hierarchy” to the empty text slots under the “MapVoter” script, you can just drag in only the mapvotes text object that the “MapVoter” script is sitting on, but you don’t have to. For example clicked on the “CubethonVotes” text object in the “Hierarchy” and then only dragged in the “CubethonVotes” into the empty text slots in the inspector under the “MapVoter” script and left the others blank.

 

Here is the script I made for the Count Down timer before the most voted scene is loaded (you right click in the Hierarchy and click “UI” and then “Text” and then you HAVE TO name it “StartCounter” and then make an Empty Game object and parent it to the StartCounter text and then add the script to the Empty Game object which I named “Counter”, then you have to click on the Counter game object and drag the “StartCounter” from the Hierarchy into the Inspector where there is a place to put a Text File under the CountDown script). :

 

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

 

public class CountDown : MonoBehaviour {

 

  public float timeRemaining = 10f;

 

  public Text text;

 

  int time, a;
  float x;
  public bool count;
  public string timeDisp;

 

  void Start()
  {
       text.text = timeRemaining + ” Seconds Remaining”;
       time = 4;
       count = false;
  }

 

  // Update is called once per frame
  void FixedUpdate()
  {
       if (count == false)
       {
            timeDisp = time.ToString();
            text.text = timeRemaining + ” Seconds Remaining”;
            x += Time.deltaTime;
            a = (int)x;
            print(a);
            switch (a)
            {
               case 0: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “10”;
               break;
               case 1: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “9”;
               break;
               case 2: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “8”;
               break;
               case 3: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “7”;
               break;
               case 4: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “6”;
               break;
               case 5: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “5”;
               break;
               case 6: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “4”;
               break;
               case 7: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “3”;
               break;
               case 8: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “2”;
               break;
               case 9: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “1”;
               break;
               case 10: GameObject.Find(“StartCounter”).GetComponent<Text>().text = “0”;
               break;
               case 11: GameObject.Find(“StartCounter”).GetComponent<Text>().enabled = false;
               count = false;
               break;
            }
        }
   }
}

 

After you attach this script click on the first map and go to “On Click()” in the “Inspector” and drag in the “Background” UI Image, that the buttons are parented to, and then click the drop down next to it and go to “MapLoader” and select “DesertEnviroClick()” (or what ever the variable for that specific map button is). You would also need to make a timer so that it didn’t load whatever map was clicked first which is the script above called “CountDown”.

 

or Click Here to download the script https://github.com/HansenDJ/Map-Voting-Project

on August 17, 2017

Thanks a lot man! Here is my mail: xxjacob@email.com

on August 27, 2017

I got it to finally show the number of votes for each map. The problem was that I actually wasn’t supposed to add an empty game object to the each of the map text objects that was holding the “MapLoader” script. I had to delete those empty game objects and then attach the script to the map text objects themselves. I also added a scene for each map image. So now the project is complete.

on August 27, 2017

None of the levels I imported, which load when they have the most votes after the timer runs out, work correctly. I don’t know how to upload them right I think. What I do, which is probably the wrong way to do it, is I open “File Explorer” on my Windows 10 computer and go to the folder with this project, and then I click on the “Assets” folder and I drag in everything from that folder, except the meta files, and let that load. If there is a way to fix this let me know. It also could be that way because each time I imported a project I would have to delete a bunch of the “Standard Assets” that were duplicates because many of the “Standard Assets” files were in all of my project so stuff was defined twice.

Show more replies
  • Liked by
Reply
Cancel
1 on August 18, 2017

HansenDJ i can’t get this to work correctly. First it told me that i should add the scene i wanted to load to the build settings, but i already had it there. 2nd everything i had in one of my scenes just disappeared and now i can’t find it (that was the scene that i tried to load)

Can you help? Thanks! 

  • Liked by
Reply
Cancel