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
2
Question by firsy · Apr 07, 2017 at 02:46 PM · networkinginstantiatespawnsetactiveunity multiplayer

How do I spawn inactive objects on new clients?

So it appears that if an object spawned with NetworkServer.Spawn is inactive on the server when a client connects, it will never be spawned on that client even if it is reactivated. Is this intended behavior? And how can i work around it, is my only option to never disable any object on the server?

Comment
Add comment · Show 1
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 RealSoftGames · Apr 08, 2017 at 06:23 AM 0
Share

have you tried spawning the object as active and then triggering it to be inactive?

This could also be a bug which may be worth infor$$anonymous$$g unity about, could you also note down what version of unity you are using, have you got some code example we could see to ensure the intended behaviour is what you think it is doing. assu$$anonymous$$g you are using UNet.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by firsy · Apr 08, 2017 at 06:50 AM

So i delved through the source code to see where exactly the distinction between inactive and active objects happens when spawning, and i found it in NetworkServer.SetClientReadyInternal, this is the part that is called only for connections that aren't the local host:

 foreach (NetworkIdentity uv in objects.Values)
 {
     if (uv == null)
     {
         if (LogFilter.logWarn) { Debug.LogWarning("Invalid object found in server local object list (null NetworkIdentity)."); }
         continue;
     }
     if (!uv.gameObject.activeSelf)
     {
         continue;
     }
 
     if (LogFilter.logDebug) { Debug.Log("Sending spawn message for current server objects name='" + uv.gameObject.name + "' netId=" + uv.netId); }
 
     var vis = uv.OnCheckObserver(conn);
     if (vis)
     {
         uv.AddObserver(conn);
     }
 }

Basically when you call NetworkServer.SetClientReady(conn), conn is only added as an observer to objects that have activeSelf == true, unless its the local connection. So we can just add the connection as an observer to the objects that are inactive, right after calling NetworkServer.SetClientReady. Heres the code im using:

 private void SetClientReadyIncludeInactive(NetworkConnection conn) {
     NetworkServer.SetClientReady(conn);
 
     //tell client to spawn inactive objects, because SetClientReady wont
     AddObserverToInactive(conn);
 }
 
 private void AddObserverToInactive(NetworkConnection conn) {
     if(conn.connectionId == 0) {
         //local host has observer added to inactive NetworkBehaviours by SetClientReady already
         return;
     }
 
     foreach(NetworkIdentity netId in NetworkServer.objects.Values) {
         if(netId == null) {
             Debug.LogError("Trying to add observer to object of null NetworkIdentity.", this);
             continue;
         }
         if(!netId.gameObject.activeSelf) {
             MethodInfo OnCheckObserver = typeof(NetworkIdentity).GetMethod("OnCheckObserver", BindingFlags.NonPublic | BindingFlags.Instance);
             MethodInfo AddObserver = typeof(NetworkIdentity).GetMethod("AddObserver", BindingFlags.NonPublic | BindingFlags.Instance);
 
             if((bool)OnCheckObserver.Invoke(netId, new object[] { conn })) {
                 AddObserver.Invoke(netId, new object[] { conn });
             }
         }
     }
 }

The only thing to worry about now is that objects will be spawned as active on the new client, but I could just use a sync var for that. Or maybe send a message to the new client with a list of all the inactive objects and then set them as inactive in the handler.

Edit: Still not sure if this is a bug or not, if not i think SetClientReady should take a bool that says whether to add the connection as an observer to inactive objects. Maybe I'll add a pull request if this approach works out well.

Comment
Add comment · Show 6 · 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 Amir-Ward · Jun 26, 2017 at 01:34 AM 0
Share

firsy what did you end up doing in the end? Gather a list of inactives and send to client?

avatar image quangthai · Oct 02, 2017 at 11:57 PM 1
Share

Where did you put SetClientReadyIncludeInactive?

avatar image chadfranklin47 quangthai · Jan 10, 2018 at 03:42 AM 0
Share

Did you figure it out?

avatar image lucasmontec · Nov 12, 2017 at 04:53 AM 0
Share

Dude, you are a total hero! Thanks for this!

avatar image chadfranklin47 lucasmontec · Jan 10, 2018 at 04:11 AM 0
Share

Where did you run SetClientReadyIncludeInactive()?

avatar image chadfranklin47 · Jan 10, 2018 at 03:42 AM 0
Share

Where did you call SetClientReadyIncludeInactive()?

avatar image
0

Answer by PigBang · Apr 04, 2018 at 09:58 AM

We has this problem for our cached objects. They disabled when in cache. For us help call NetworkServer.Spawn() after each reuse object.

Comment
Add comment · 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

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

10 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

Related Questions

[UNET] How to know the connectionID of a player ? 2 Answers

No appropriate version of 'UnityEngine.Object.Instantiate' 0 Answers

How to spawn gameobject from host to all clients 1 Answer

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer

UNET NetworkServer.Spawn intermittently not called on clients 1 Answer


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