- Home /
How can I transfer a child object from one player to another in unity multiplayer using Unity Networking?
I am trying to transfer a key from player one to player two when player two attacks player one. The logic I am using is I'm trying to deactivate key on player one when player two's sword collides 2 times to player one and at the same time activating the key of player two. The problem is the key disappears from one player but does not appear on other player. I have tried many different logic, but those didn't give me desired result.
The script for player (named Hero):
public class PlayerControler : NetworkBehaviour {
public static PlayerControler instance;
[SerializeField]
private float moveSpeed = 10.0f;
private Animator anim;
public float speedH = 6.0f;
private float yaw = 0.0f;
private bool attacked;
[SyncVar]
public int grip = 2;
public int attackPower = 1;
[SyncVar]public GameObject keyIdentity;
public bool hasKey;
[SyncVar]
public bool keyDropped;
void Awake()
{
if (instance == null)
{
instance = this;
}
attacked = false;
hasKey = false;
keyDropped = false;
}
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
keyIdentity.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.Self);
yaw += speedH * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(0.0f, yaw, 0.0f);
CmdMove(transform.position);
if (moveDirection == Vector3.zero)
{
anim.SetBool("IsWalking", false);
}
else
{
anim.SetBool("IsWalking", true);
}
if (Input.GetMouseButtonDown(0))
{
anim.Play("DoubleAttack");
}
}
void OnTriggerEnter(Collider target)
{
if (target.gameObject.tag == "Weapon")
{
attacked = true;
TakeDamage();
}
if (target.gameObject.tag == "Key")
{
Destroy(target.gameObject);
keyIdentity.SetActive(true);
hasKey = true;
GameObject mainGate = GameObject.FindGameObjectWithTag("Gate");
Destroy(mainGate);
}
if (target.gameObject.tag == "Cup")
{
target.gameObject.SetActive(false);
GameManager.instance.playPanel.SetActive(true);
Time.timeScale = 0.0f;
}
}
public override void OnStartLocalPlayer()
{
Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
}
[Command]
void CmdMove(Vector3 position)
{
// we trust the player :)
transform.position = position;
SetDirtyBit(1u);
}
public override bool OnSerialize(NetworkWriter writer, bool initialState)
{
writer.Write(transform.position);
return true;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
if (isLocalPlayer)
{
return;
}
transform.position = reader.ReadVector3();
}
void TakeDamage()
{
if (!isServer)
return;
grip -= attackPower;
if (grip <= 0)
{
grip = 0;
//transfer key
if (hasKey)
{
hasKey = false;
RpcDropKey();
}
RpcChKey();
}
}
[ClientRpc]
void RpcDropKey()
{
keyIdentity.SetActive(false);
}
[ClientRpc]
void RpcChKey()
{
keyDropped = true;
}
}
Script for Swords:
public class WeaponScript : MonoBehaviour {
public static WeaponScript instance;
void Start()
{
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Hero")
{
KeyAfterAttack();
}
}
void KeyAfterAttack()
{
if(PlayerControler.instance.grip == 0)
PlayerControler.instance.keyIdentity.SetActive(true);
}
}
Answer by Salanyel · Jul 27, 2017 at 02:55 PM
From the code, you're setting the keyIdentity to true when you are hitting the character the second times.
On the PlayerController, you probably set the variable to "true", I need information about this. But nowhere you use this value to show the key. You have a "True" to the key, but you do not activate the corresponding game object. You need a OnChange event on your variables or to add a function in your update to detect a change and display the key.
No, I haven't set the variable to true. I don't know what is OnChange event. Would you please modify my code to explain me how can I apply this?
It looks like the following code
if (target.gameObject.tag == "$$anonymous$$ey") { Destroy(target.gameObject); keyIdentity.SetActive(true); has$$anonymous$$ey = true; GameObject mainGate = GameObject.FindGameObjectWithTag("Gate"); Destroy(mainGate); }
is destroying your gameobject. It is probably not accessible after one step in this function.
Actually, that key is different object with tag "$$anonymous$$ey". This object is keyIdentity with tag "key$$anonymous$$eeperIdentity". When player hits the "$$anonymous$$ey" the key is destroyed and keyIdentity turns active.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131317im_/https://answers.unity.com/themes/thub/images/avi.jpg)