- Home /
Whats wrong with my RPC,!showing clothes on the network
Im trying to build an online game. It is built for small groups of people. I use C# to send RPC's and im updating as expected except for one thing. When i toggle my hat or backpack(currently the only things in game that can be tested) they toggle the renderers of the other player online. In single player the code works fine. Am i targetting the other player some how without knowing? Or is this a result from the following codes?
Edit: Now i have fixed the problem for turning on other players renderers, but now it doesnt change any renderer of any player at all. I have networkview on my player for its transfor, inventory, and player script. im instantiating clones of the prefab for all players. I tried to run checks for ownership but so far nothing has worked. Im completely lost.
I erased old code from this post to keep it clean.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Inventory : MonoBehaviour{
public List<Item> items;
public List<GameObject> fashion;
public NetworkViewID netViewId;
public MyPlayerEngine playerScript;
public GUISkin customSkin;
private Rect windowRect = new Rect(0, 0, 200, 350);
private Rect windowRect1 = new Rect(0, 0, 200, 214);
private Rect windowRect2 = new Rect(0, 0, 106, 80);
public bool invenOpen= false;
public bool characterMenu = false;
public bool confirmMenu = false;
private Vector2 buttonSize = new Vector2 (120, 20);
private int activeSlot;
public int gold = 0;
public Vector2 scrollPosition;
//fashion items
public GameObject hat;
public GameObject sack;
void Start(){
netViewId = GetComponent<NetworkView>().viewID;
Debug.Log (netViewId);
if(items == null){
clearInventory();
}
if(fashion == null){
fashion = new List<GameObject>();
}
////////Name////price/type/subtype/value/value2/description/quantity/equippedornot
AddItem("Beef", 10, "Food", "Food", 10, 0, 0, "Restores 20 Hp to the user.", 3, false);
AddItem("Cheese", 5, "Food", "Food", 5, 0, 0, "Restores 20 Hp to the user.", 1, false);
AddItem("Reg Potion", 3, "Food", "Potion", 3, 0, 0, "Restores 3 Hp/s for 60 seconds.", 1,false);
AddItem("Rusty Sword", 1, "Weapon", "Melee", 5, 3, 0, "A dull and rusty sword.", 1,false);
AddItem("Rusty Axe", 1, "Weapon", "Melee", 6, 2, 0, "A dull and rusty axe.", 1,false);
//AddItem ("Old Bow", 1, "Weapon", "Ranged", 3, 0, 0, "A worn and rigged old bow.", 1, false);
AddItem ("Old Staff", 1, "Weapon", "Magic", 3, 5, 0, "A worn and rigged old staff.", 1, false);
AddItem ("Leather Armor", 1, "Armor", "Armor", 10, 5, 0, "A basic armor made of leather.", 1, false);
AddItem ("Cloth Armor", 1, "Armor", "Armor", 5, 10, 0, "A basic armor made of cloth.", 1, false);
AddItem ("Hat", 1, "Head", "Head", 0, 0, 0, "A fake hat.", 1, false);
AddItem ("Pack", 1, "Back", "Back", 0, 0, 1, "A fake backpack.", 1, false);
fashion.Add (hat);
fashion.Add (sack);
}
void Update(){
if(Input.GetKeyUp (KeyCode.I)){
invenOpen = !invenOpen;
}
if(Input.GetKeyUp (KeyCode.C)){
characterMenu = !characterMenu;
}
}
Rect ClampToScreen(Rect r)
{
r.x = Mathf.Clamp(r.x,0,Screen.width-r.width);
r.y = Mathf.Clamp(r.y,0,Screen.height-r.height);
return r;
}
void OnGUI(){
GUI.skin = customSkin;
if(invenOpen){
windowRect = ClampToScreen(GUI.Window(0,windowRect,DoInventoryWindow,"Inventory"));
}
if(characterMenu){
windowRect1 = ClampToScreen(GUI.Window(1, windowRect1, DoInventoryWindow2, "Character"));
}
if(confirmMenu){
windowRect2 = ClampToScreen(GUI.Window(2, windowRect2, DoInventoryWindow3, "" + items[activeSlot].itemName));
}
}
public void AddItem(string name, int price, string type, string subType, int value, int value2, int modelNumber, string info, int quantity, bool equipped){
bool isInList = false;
for(int I = 0; I < items.Count; I++){
if(items[I].itemName == name){
items[I].quantity += quantity;
isInList = true;
}
}
// Outside for loop
if (isInList == false){
Item ni = new Item();
ni.itemName = name;
ni.price = price;
ni.type = type;
ni.subType = subType;
ni.value = value;
ni.value2 = value2;
ni.modelNumber = modelNumber;
ni.info = info;
ni.quantity = quantity;
ni.equipped = equipped;
items.Add (ni);
}
}
void useItem(int i){
if(items[i].type == "Food"){
Debug.Log ("Used "+ items[i].itemName);
playerScript.TakeHeal (items[i].value);
if(items[i].quantity == 1){
items.Remove(items[i]);
}
else{
items[i].quantity--;
}
}
if(items[i].type == "Head"){
foreach(Item item in items){
int x;
x = items[i].modelNumber;
if(item.equipped == true && item.type == "Head"){
UnEquipFashion(x);
item.equipped = false;
break;
}
if(item.equipped == false && item.type == "Head"){
EquipFashion(x);
item.equipped = true;
break;
}
}
}
if(items[i].type == "Back"){
foreach(Item item in items){
int x;
x = items[i].modelNumber;
if(item.equipped == true && item.type == "Back"){
UnEquipFashion(x);
item.equipped = false;
break;
}
if(item.equipped == false && item.type == "Back"){
EquipFashion(x);
item.equipped = true;
break;
}
}
}
}
public void LoadAppearance(){
foreach(Item item in items){
int x;
x = item.modelNumber;
if(item.equipped == true && item.type == "Head"){
GetComponent<NetworkView>().RPC("EquipFashion", RPCMode.All, x);
}
if(item.equipped == true && item.type == "Back"){
GetComponent<NetworkView>().RPC("EquipFashion", RPCMode.All, x);
}
}
}
[RPC]public void UnEquipFashion(int x){
if(GetComponent<NetworkView>().isMine){
fashion[x].transform.GetComponent<Renderer>().enabled = false;
}
}
[RPC]public void EquipFashion(int x){
if(GetComponent<NetworkView>().isMine){
fashion[x].transform.GetComponent<Renderer>().enabled = true;
}
}
public void clearInventory(){
items = new List<Item>();
}
//INVENTORY WINDOW
void DoInventoryWindow(int windowID){
GUI.Box (new Rect (2, 328, 124, 20), "Gold: " + gold);
scrollPosition = GUI.BeginScrollView(new Rect(2, 23, 120, 302), scrollPosition, new Rect(0, 10, buttonSize.x, items.Count * buttonSize.y));
if(items.Count >= 1){
for(int i = 0; i < items.Count; i++){
if(items[i].equipped){
if (GUI.Button (new Rect (0, 12 + i * buttonSize.y, buttonSize.x, buttonSize.y), items[i].itemName + " x" + items[i].quantity + "◄")){
activeSlot = i;
confirmMenu = true;
}
}
else{
if (GUI.Button (new Rect (0, 12 + i * buttonSize.y, buttonSize.x, buttonSize.y), items[i].itemName + " x" + items[i].quantity)){
activeSlot = i;
confirmMenu = true;
}
}
}
}
GUI.EndScrollView();
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
void DoInventoryWindow2(int windowID){
GUI.Box(new Rect(98,20,100,20), "Melee Atk "+ playerScript.attack);
GUI.Box(new Rect(98,40,100,20), "Magic Atk "+ playerScript.magic);
GUI.Box(new Rect(98,60,100,20), "Ranged Atk "+ playerScript.ranged);
GUI.Box(new Rect(98,80,100,20), "Defense "+ playerScript.defense);
GUI.Box(new Rect(98,100,100,20), "Strength "+ playerScript.strength);
GUI.Box(new Rect(98,120,100,20), "Vitality "+ playerScript.vitality);
GUI.Box(new Rect(98,140,100,20), "Speed "+ playerScript.speed);
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
void DoInventoryWindow3(int windowID){
playerScript.msgLog = "" + items[activeSlot].info;
//GUI.Box(new Rect(Screen.width / 2 - 53, Screen.height / 2 - 69 ,106,20), "Actions");
if (GUI.Button (new Rect (3, 20, 100, 20), "Use")){
useItem(activeSlot);
playerScript.msgLog = "";
confirmMenu = false;
}
if (GUI.Button (new Rect (3, 40, 100, 20), "Discard All")){
items.Remove(items[activeSlot]);
confirmMenu = false;
playerScript.msgLog = "";
}
if (GUI.Button (new Rect (3, 60, 100, 20), "Cancel")){
playerScript.msgLog = "";
confirmMenu = false;
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
}
$$anonymous$$y code changed a little when i updated to Unity 5. However it works the same. The same error still occurs. I think it has something to do with the 6th and 11th lines in my script above. Its calling this.gameObject but then it places the Items on the wrong player. So is it possible that I'm not actually targeting my own object some how?
First, I assume you have for each character separate network view. So first verify if the RPC is really sent from the proper character. What is the difference between items and fashion.
I put a network view on my player prefab and instantiate it on start. $$anonymous$$y rpcs do work, because i can lower health values of the other players. I can change "Hat" to be equipped, and it equips onto the other player on both client and server. Items are my text and int values for anything a player may own, fashion is just the 3d objects that you can see. Items is of type item, fashion is of type GameObject.
using UnityEngine;
using System.Collections;
public class Item {
public string itemName { get; set; }
public int price { get; set; }
public string type { get; set; }
public string subType { get; set; }
public int value { get; set; }
public int value2 { get; set; }
public int modelNumber { get; set;}
public string info { get; set; }
public int quantity { get; set;}
public bool equipped { get; set; }
}
this is my item class, if equipped is "true" it should render the object at fashion[modelNumber]
The important is if the rpc that you have unequiped the item is called on the proper player. What happens if you have 3 players? If it stills check on one player than my clue is wrong. (Didn't see the second response ^^)If I am not mistaken, you use dictionary (or something like it) to store those gameObjects. Could you post the line 6 or 11? There should be GetComponent ins$$anonymous$$d of networkView.