[PUN] Change Parent's GameObject in Network
Hello, I'm Developing Medieval Multiplayers Game and I want the player have the opportunity to take his sword in hands or not.
So i maked two empty GameObject to represent the Scabbard and the Hand Position (One Children of left leg's character and one Children of right hand's character).
When the Player Click on left button, this call Hand function (Sword take the position of Hand Empty GameObject) and the variable inHand is true. And if the player press 'R' key, this call Scabbard function (Sword Take the position of Scabbard Empty Gameobject Transform) and the variable inScabbard is true.
So there is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeleeFight : MonoBehaviour {
Animator Animation;
PhotonView View;
public GameObject MeleeWeapon;
public Transform ScabbardPosition;
public Transform HandPosition;
public bool inScabbard = true;
public bool inHand = false;
void Start () {
Animation = GetComponent<Animator>();
View = GetComponent<PhotonView>();
inScabbard = true; //Set the weapon in Scabbard
}
void Update () {
//Check Status
if (inScabbard) //Weapon in Scabbard
{
View.RPC("Scabbard", PhotonTargets.All);
if (Input.GetMouseButtonDown(0)) //To Hand
{
if (View.isMine)
{
Animation.SetBool("Armed", true);
inScabbard = false;
inHand = true;
}
}
}
if (inHand) //Weapon in Hand
{
View.RPC("Hand", PhotonTargets.All);
if (Input.GetKeyDown(KeyCode.R)) //To Scabbard
{
if (View.isMine)
{
Animation.SetBool("Armed", false);
inHand = false;
inScabbard = true;
}
}
}
}
[PunRPC] void Scabbard() //Weapon in Scabbard
{
if (View.isMine)
{
MeleeWeapon.transform.position = ScabbardPosition.position;
MeleeWeapon.transform.rotation = ScabbardPosition.rotation;
MeleeWeapon.transform.SetParent(ScabbardPosition);
}
}
[PunRPC] void Hand() //Weapon in Hand
{
if (View.isMine)
{
MeleeWeapon.transform.position = HandPosition.position;
MeleeWeapon.transform.rotation = HandPosition.rotation;
MeleeWeapon.transform.SetParent(HandPosition);
}
}
}
But it does not work, when a Player take his Sword, other Player don't see the Sword in his Hand (But the animation work). The Sword stay in Scabbard position for others Players.
Thank you in advance and sorry for my bad English.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121310im_/https://answers.unity.com/themes/thub/images/avi.jpg)