- Home /
Syncronize Items over network
How would I go about instantiating an item on the network that everyone can pick up and add to their inventory? I have a system that works flawlessly in singleplayer but once someone connects it stops working. Also how would one manage to synchronize a object added as a child of a bone so that all can see? Here are my attempts:
This is the inventory script on the player prefab (pardon my messy code):
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
bool invOpen=false;
int x=1,y=1;
ArrayList itemsInInventory=new ArrayList(100);
// Use this for initialization
void Start () {
}
bool ticked;
// Update is called once per frame
void Update () {
if (Input.GetButton("Inventory"))
{
if (!ticked)
{
ticked=true;
if (invOpen)
{
(GetComponent("Esc")as Esc).PauseGame(false);
invOpen=false;
}
else
{
(GetComponent("Esc")as Esc).PauseGame(true);
invOpen=true;
}
}
}
else
ticked=false;
GameObject[] loot = GameObject.FindGameObjectsWithTag("Loot");
int i;
for(i=0;i<loot.Length;i++)
{
if(Vector3.Distance(loot[i].transform.position,transform.position)<15&&Input.GetKey(KeyCode.Q))
{
if (networkView.isMine)
Add ((loot[i].GetComponent("Item")as Item).pickup());
}
}
}
void Add(GameObject item)
{
if(item!=null)
itemsInInventory.Add(item);
}
void CloseGui()
{
}
void OnGUI()
{
if (invOpen)
{
GUI.Window(1,new Rect(x,y,500,700),InvWindow,"Inventory");
}
}
void InvWindow (int id)
{
int i,j=0,k=0;
for(i=1;i-1<itemsInInventory.Count;i++)
{
if(GUI.Button(new Rect((x)*(j*100),(y)*(k*100)+15,100,100),(((GameObject)itemsInInventory.ToArray()[i-1]).GetComponent("Item")as Item).ID))
{
if (networkView.isMine)
(((GameObject)itemsInInventory.ToArray()[i-1]).GetComponent(typeof(Item))as Item).Equip((GetComponentInChildren(typeof(RightHandMount))as RightHandMount).getTransform(RightHandMount.Mount.RightHand));
//((Item)Instantiate((Item)itemsInInventory.ToArray()[i-1])).drop(transform.position);
//itemsInInventory.Remove(itemsInInventory.ToArray()[i-1]);
}
j++;
if(j==5)
{
j=0;
k++;
}
}
GUI.DragWindow ();
}
}
Then this is my Item script:
using UnityEngine;
using System.Collections;
public class Item : MonoBehaviour {
public enum EquipSlots{
LeftHand,
RightHand,
Head
}
public string ID = "";
public bool IsEquipable = false;
public EquipSlots Slot = EquipSlots.RightHand;
bool isdrop = true;
// Use this for initialization
void Start () {
}
public GameObject pickup()
{
if(isdrop)
{
isdrop=false;
gameObject.SetActive(false);
return gameObject;
}
else return null;
}
public void drop(Vector3 position)
{
isdrop=true;
transform.position=position;
gameObject.SetActive(true);
}
public GameObject Equip(Transform parent)
{
transform.parent=parent;
transform.localPosition=Vector3.zero;
gameObject.SetActive(true);
print ("sldkf");
return this.gameObject;
}
void OnMouseEnter()
{
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
int i;
for(i=0;i<players.Length;i++)
{
if(Vector3.Distance(players[i].transform.position,transform.position)<7)
{
highlight(true);
}
else
highlight(false);
}
}
void OnMouseExit()
{
highlight(false);
}
void OnMouseDown()
{
}
void highlight(bool highlight)
{
}
// Update is called once per frame
void Update () {
}
}
The highlight will be implemented later, and the mounts will not be individual scripts. Any help would be much appreciated.
Answer by Fattie · Feb 21, 2013 at 09:42 AM
http://answers.unity3d.com/questions/326626/games-with-multiple-maps.html
note the "important note" and "critical note" and pls vote up useful answers! :)
Your answer
