Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ParthDave · Jul 27, 2017 at 01:57 PM · ontriggerentermultiplayer-networkingsetactivesynchronizationclient-server

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.

alt text

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);
     }
   
 }


mazequestshot.png (271.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ParthDave · Jul 27, 2017 at 03:52 PM 0
Share

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?

avatar image Salanyel · Jul 27, 2017 at 04:41 PM 0
Share

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.

avatar image ParthDave Salanyel · Jul 28, 2017 at 04:55 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity NetworkTransformChild - Syncing the Child of a Child 0 Answers

Syncing random player colors through PUN2 1 Answer

Multiplayer - non player object synchronisation 0 Answers

Photon Int sync not working 2 Answers

Client side prediction, rigidbody.velocity, photon network 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges