- Home /
RPC activating gameobject on Host instead of on the Client
Hi guys. I am trying to make a player put on a helmet when clicking on it. There is a helmet on the ground tagged as "Helmet", and the another helmet on the player prefab with the renderers deactivated so it is invisible, tagged as "Object".
What I try to do with my code is to click on the helmet on the floor, make it unactive, and then set the renderers on for the helmet on the head of the player that has clicked.
What is happening to me is that when my Host clicks the helmet on the floor everything works perfect, but when it's the Client who clicks, the helmet that turns active it is still the one on the Host head.
PD: This script is turned on on for the local player through another script that enables it using the function "OnStartLocalPlayer"
Here's my script :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Helmet : NetworkBehaviour {
public Transform cameraTransform;
GameObject helmetFloor;
GameObject helmetHead;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Vector3 rayPos = cameraTransform.position + cameraTransform.forward * 1f;
Debug.DrawRay(rayPos, cameraTransform.forward * 5f, Color.green, 0.5f);
if (Physics.Raycast(rayPos, cameraTransform.forward, out hit, 5f))
{
if (hit.transform.tag == "Helmet")
{
if (Input.GetMouseButtonDown(0))
{
CmdHelmet();
}
}
}
}
[Command]
void CmdHelmet()
{
RpcHelmet();
}
[ClientRpc]
void RpcHelmet()
{
helmetFloor = GameObject.FindGameObjectWithTag("Helmet");
helmetHead = GameObject.FindGameObjectWithTag("Object");
helmetFloor.SetActive(false);
helmetHead.GetComponent<Renderer>().enabled = true;
}
}
This is for my thesis at university (yeah, it got weird), so all the help would be much appreciated. Thanks!
Guys I solved it! I don't know why I thought about this because I think it was a long shot but it worked!
I declared a public GameObject helmetHead, and I put it in the inspector ins$$anonymous$$d of using the find with tag function. I guess that since once you are a player you cannot access the other players helmets, the script has no other option than to enable yours. :D
Your answer
Follow this Question
Related Questions
RPC isn't called on objects with another name, even though they have the same script. 1 Answer
View ID AllocatedID: # not found during lookup. Strange behaviour may occur 1 Answer
Unet [command] doesn't get called at all. 0 Answers
How to use an RPC to send an animation over the network? 2 Answers
Whats wrong with my RPC,!showing clothes on the network 0 Answers