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 /
  • Help Room /
avatar image
0
Question by Soloman23 · Mar 01, 2016 at 02:27 PM · networkingserverclientlocal

Need help spawning a gameObject on the server UNET

I really hope I can get an answer for this one!

My issue is I'm trying my best to spawn an object for each player on the server, but the only thing is it continues to return null when a non-local player joins the game

here's a bit of code showing my issue

 void Start()
 {
       
            if (isLocalPlayer)
         {
             if (otherSettings.startWeapon)
             {
                 TransmitStartWeapon(otherSettings.startWeapon);
             }
         }
 }
 
 
 [ClientCallback]
     void TransmitStartWeapon(GameObject wep)
     {
         if (isLocalPlayer)
         {
             Cmd_SpawnStartWeapon(wep);
         }
     }
 
  [Command]
     void Cmd_SpawnStartWeapon(GameObject wep)
     {
         GameObject newWep = Instantiate(wep, transform.position, transform.rotation) as GameObject;
         weaponH.currentWeapon = newWep.GetComponent<Weapon>();
         NetworkServer.Spawn(newWep);
     }


The weapon is registered as a spawnable prefab, and this script is attached to the player. I pray you guys can actually get baack to me with this question, I've never had anyone answer me on here :/

Comment
Add comment · Show 4
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 Salmjak · Mar 01, 2016 at 05:17 PM 1
Share

Does the weapon prefab itself have a NetworkIdentity? It should have a NetworkIdentity component. Also, what are you doing with weaponH.currentWeapon = newWep.GetComponent<Weapon>(); Are you setting a parent or a child or something? Because that won't work and could be the cause of your error.

avatar image Soloman23 Salmjak · Mar 01, 2016 at 06:15 PM 0
Share

To explain it simply the weaponH (or weaponHandler) adds this weapon to a list and the weapon component parents it to the players hand, also the weapon in fact does have a network identity, I have two questions...

  1. Does the weapon itself need to have a local player authority or a server only authority.

  2. How would I go about doing the parenting of the obj, because I have a theory... What if I just attached a script that that handles syncing the parent object and also the position and rotation of the weapon. Is there any way to do this, it would be much simpler.

Thanks for getting back to me!

Also I might add that the error appears here GameObject newWep = Instantiate(wep, transform.position, transform.rotation) as GameObject;

avatar image Salmjak Soloman23 · Mar 03, 2016 at 01:45 PM 1
Share

@Soloman23 When you want to set a parent for a NetworkObject you have to keep a reference to a NetworkIdentity of the parent.

The weapon itself shouldn't have any boxes checked (so neither local nor server) :)

I've had some problems with setting the parent, but the short version is: You need to pass a reference to the NetworkIdentity of the parent gameObject to a [Command], set the parent and then pass the reference to a [ClientRpc] and set the parent.

To set the parent from the NetworkIdentity you have to (depending on your hierarchy setup) find the child of the gameobject with networkidentity. Remember that FindChild() only finds DIRECT children, so not grandchildren (which your hand probably is). There are ways to do this, either by an iterative search through all children or by writing the full path to the child (ex "Spine/Arm/Forearm/Hand").

So again: You set the weapons parent to the hand on client. You then send a reference to the networkidentity of the gameobject (which probably is the players gameobject) to a [Command]. You then use the networkidentity to retrieve the gameobject and use FindChild() to retrieve the hand and then set the weapons parent to the hand. Now you do the same in a [ClientRpc] which is called from the [Command].

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Soloman23 · Mar 01, 2016 at 10:37 PM

So I've been trying and trying to get this to operate correctly, and I've come this far:

Well basically I created a new SyncVar that is a gameObject, and saying in code: GameObject wep = (GameObject)Instantiate(Resources.Load(startWeapon.name, typeof(GameObject)), transform.position, transform.rotation);, and this is in fact working correctly and is instantiating the weapon, but the problem is when assigning my syncVar to that instantiated object, the SyncVar still returns null?!!?! I'm so confused I wish I could get an answer on here, what I'm trying to achieve is so simple.

Comment
Add comment · Show 4 · 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 Soloman23 · Mar 01, 2016 at 10:39 PM 0
Share

Never$$anonymous$$d totally got this part to work here's a bit of code showing the new way I'm doing this :

 [Server]
     void Cmd_PassStartWeapon(GameObject startWeapon)
     {
         GameObject wep = (GameObject)Instantiate(Resources.Load(startWeapon.name, typeof(GameObject)), transform.position, transform.rotation);
         NetworkServer.Spawn(wep);
         syncStartWeapon = wep;
         weaponH.currentWeapon = syncStartWeapon.GetComponent<Weapon>();
     }
avatar image Soloman23 · Mar 01, 2016 at 10:47 PM 0
Share

So now this is the issue, I'm receiving 2 null references

  void Start()
         {
             #region Spawn start weapon into world.
             TransmitStartWeapon();
             SetStartWeapon();
             #endregion
          }
     
     void SetStartWeapon()
         {
             if(!isLocalPlayer)
             {
                 weaponH.currentWeapon = syncStartWeapon.GetComponent<Weapon>();
             }
         }
     
     [Command]
         void Cmd_PassStartWeapon(GameObject startWeapon)
         {
             GameObject wep = (GameObject)Instantiate(Resources.Load(startWeapon.name, typeof(GameObject)), transform.position, transform.rotation);
             NetworkServer.Spawn(wep);
             syncStartWeapon = wep;
             weaponH.currentWeapon = syncStartWeapon.GetComponent<Weapon>();
         }
     
     [ClientCallback]
         void TransmitStartWeapon()
         {
             if (isLocalPlayer)
             {
                 Cmd_PassStartWeapon(otherSettings.startWeapon);
             }
         }
 

The null references originate here: GameObject wep = (GameObject)Instantiate(Resources.Load(startWeapon.name, typeof(GameObject)), transform.position, transform.rotation);

And here: weaponH.currentWeapon = syncStartWeapon.GetComponent<Weapon>();

avatar image Salmjak Soloman23 · Mar 03, 2016 at 01:48 PM 1
Share

@Soloman23 Don't pass the gameobject to the [Command] just pass the string gameObject.name directly. I think that will solve your first error. The second error should be solve either by this or my other answer I typed recently. But probably by this, the second error seem to be due to the instantiation failing, and thus there is no gameobject to use GetComponent() on.

avatar image Soloman23 Salmjak · Mar 03, 2016 at 04:38 PM 0
Share

I want to redirect you to a new discussion that explains this further, thanks for this though it solved to of my issues, but in doing so opened a door to a new one. http://answers.unity3d.com/questions/1149700/need-help-with-syncing-gameobject-unet.html#comment-1149743

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

66 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

Related Questions

Unet: [Command] not running 1 Answer

Spawning Dynamically Created Object (Network) 1 Answer

How Unity Object Tags working on multiplayer games? 0 Answers

non-player object can't execute's "[Command]"'s attributes 0 Answers

UDPClient can't receive and send ? 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