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*.transform.position,transform.position)<15&&Input.GetKey(KeyCode.Q))*
-
{*
-
if (networkView.isMine)*
_ Add ((loot*.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)(j100),(y)(k100)+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*.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.