I have several prefab’ed planes which form “blocks” of terrain. I want to store how each level/map should look in a two-dimensional array of numbers. I am having trouble accessing this information from different classes. Currently the game only spawns the terrain once a connection is made (hosting or connecting to host). At least that is how I want it to act.
map = new int[,]{
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
};
that is in the “Area_01” script (C#), where map is a public static int[,] declared at the top.
//spawning
void OnServerInitialized()
{
SpawnTerrain();
SpawnPlayer();
}
void SpawnTerrain()
{
for(int x = 0; x < map.GetLength(0); x++)
{
for(int y = 0; y < map.GetLength(1); y++)
{
int piece = map[x, y];
switch(piece)
{
default:
break;
}
}
}
}
Ok, I got it working… here are the two scripts.
using UnityEngine;
using System.Collections;
public class Maps : MonoBehaviour {
private static int[,] level1;
public static int[,] Level1{
get {return level1;}
set {level1 = value;}
}
// Use this for initialization
void Start () {
level1 = new int[,]{
{1, 1, 1},
{1, 1, 1}
};
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MultiplayerManager : MonoBehaviour {
public string gameName = "Aethuras Blight";
public string friendName = "Sleete";
public HostData[] roomList;
public GameObject player;
void Start()
{
}
public void StartServer()
{
Network.InitializeServer (4, 56700, !Network.HavePublicAddress());
MasterServer.RegisterHost (gameName, friendName);
}
public void RefreshHostList()
{
MasterServer.RequestHostList(gameName);
}
public void OnMasterServerEvent(MasterServerEvent e)
{
if(e == MasterServerEvent.HostListReceived)
{
roomList = MasterServer.PollHostList();
}
}
public void JoinServer(HostData hd)
{
Network.Connect(hd);
}
//spawning
void OnServerInitialized()
{
SpawnTerrain();
SpawnPlayer();
}
void OnConnectedToServer()
{
SpawnPlayer();
}
void SpawnTerrain()
{
int[,] map = Maps.Level1;
for(int x = 0; x < map.GetLength(0); x++)
{
for(int y = 0; y < map.GetLength(1); y++)
{
int piece = map[x, y];
switch(piece)
{
case 1:
GameObject mapPiece = Resources.Load<GameObject>("Prefabs/Zones/L1T1");
PrefabUtility.InstantiatePrefab(mapPiece as GameObject);
break;
default:
break;
}
}
}
}
void SpawnPlayer()
{
Network.Instantiate(player, new Vector3(0, 1, 0), Quaternion.identity, 0);
}
//User Interface goodies
private void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(10, 10, 250, 75), "Host"))
{
StartServer();
}
if(GUI.Button(new Rect(265, 10, 250, 75), "Join"))
{
RefreshHostList();
}
if(roomList != null)
{
for(int i = 0; i < roomList.Length; i++)
{
if(GUI.Button(new Rect(Screen.width - 260, Screen.height * 0.1f * i + 10, 250, 75), roomList*.gameName))*
_ JoinServer(roomList*);_
_ }_
_ }_
_ }_
_ }_
_} _
_}*_
A few things to note, for those who are doing something like what I am doing:
The Resource.Load HAS to be the type you selected, I was trying to load a place even though I was declaring a GameObject. I made an empty one and childed all the planes to it, then made a prefab out of it. The other issue I was running in to, is I needed to drag the maps script to the camera, so the game was actually loading it. With out doing that I kept getting a null reference.