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 Ruffian0305 · Nov 09, 2015 at 10:23 PM · networkingspawningspawning problemsserversidenetwork instantiate

NetworkServer.Spawn Deleting Objects

Hello, I have looked around many places to try and find the answer to this problem, and it appears that no one else is seeming to have this particular trouble.

I am trying to spawn some objects on the server (which the clients should see just appear on their screen).

I know that I need NetworkServer.Spawn(GameObject go) to do this, and it works (meaning it spawns the object).

However, 1 frame later they are all gone and I have no idea why!

     [Server]
     void SpawnAll()
     {
         for (int i = 0; i < 10; i++)
         {
             Debug.Log("Spawning...");
             GameObject obj = Instantiate(obstaclePrefab);
             NetworkServer.Spawn(obj);
         }
 
         Destroy(this.gameObject);
     }

When the level is loaded, that is called. It spawns the 10 objects, but then they are immediately destroyed or deleted or something happens to them. There are no errors that are occuring in the console, they just disappear.

If I run the server as a dedicated server the objects will persist (sort of_. Connecting a client to that LAN Server will then spawn errors (Failed to spawn server object).

I tried adding the prefab to the spawnable objects on the network manager, I tried registering the prefab via code, I tried loading the prefab in via code, the prefab DOES have a network identity (and a network transform). I tried turning on local player authority, server only, both, and none.

I'm using the default network manager (mostly) for now, with the default network manager HUD. When I click Start Host (H) if I quickly pause unity as fast as possible (ctrl+shift+p) I see this: alt text

Yay cubes!

When I click the next frame button (Not unpause, just next frame) I see this (Good bye cubes T_T): alt text

I have no inkling of why this is happening (note it happens with or without the Destroy(this.gameObject), or even if I just spawn 1 object instead of 10 in the loop).

Any help at all would be greatly appreciated!

weird02.png (101.5 kB)
weird01.png (106.2 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dibbie · Nov 09, 2015 at 10:30 PM

This is how your script is working...

When SpawnAll is called, it will run through your for loop, spawn your stuff

So now, once your for loop is done... You want it to delete your object

Now, if its something thats only called once (like in Start or something), then really, only the last object should be deleted. If its something thats constantly being called (like in Update), then your probably going to experience something like that. Also, if you have this script on your prefab your spawning, its going to spawn the prefab, then delete itself, but every prefab has the same script, so their also going to delete themselves.

Im actually not too sure why youd want to delete one object after you want to spawn a bunch of objects, so try making that delete more specific, like delete only if theres more then 9 of the same object/tag in the scene or something.

Comment
Add comment · Show 1 · 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 Ruffian0305 · Nov 09, 2015 at 10:34 PM 0
Share

The script is on a throwaway script, just meant to spawn in the 10 objects and then never again do anything. This script is not on the prefab, its on an empty game object just hanging out in the scene.

     [Server]
     void SpawnAll()
     {
         for (int i = 0; i < 10; i++)
         {
             Debug.Log("Spawning...");
             GameObject obj = Instantiate(obstaclePrefab);
             NetworkServer.Spawn(obj);
         }
 
         //Destroy(this.gameObject);
     }

Produces the same result (the objects are there and then destroyed). I really appreciate your help though!!

avatar image
0

Answer by seanr · Nov 10, 2015 at 02:08 PM

probably the scene is still changing. The engine thinks those objects are in the old scene, so it destroys them.

wait until the new scene is fully loaded.

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

40 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

Related Questions

Why are objects only spawning on host but not on remote clients? 3 Answers

problem with set tag in unity network 0 Answers

Question about Unet and spawning 0 Answers

UNET Different size of spawned object on host and client 1 Answer

Networking: Standalone Simulation Server 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