• 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 Azound · Nov 02, 2010 at 05:12 AM · networking

Problem instantiating object over a network

When I try to instantiate a prefab over the network, I do not see the objects appear, and I see the following in the client's log:

Receiving state for an object whose network view exists but the observed object no longer exists

The code looks roughly like this:

public class MyClass : MonoBehaviour { public Transform CarPrefab;

public void Awake() { DontDestroyOnLoad(this); networkView.group = 1; }

 public void OnGUI() {
     if (Layout.Button("Create Car")) {
         networkView.RPC("LoadLevel", RPCMode.AllBuffered);
         CreateCar();
     }
 }

 [RPC]
 public void LoadLevel() {
         this.lastLevelPrefix = lastLevelPrefix;
 Network.SetSendingEnabled(0, false);
 Network.isMessageQueueRunning = false;

 Network.SetLevelPrefix(lastLevelPrefix);
 Application.LoadLevel("mylevel");

 Network.isMessageQueueRunning = true;
 Network.SetSendingEnabled(0, true);
 } 

 public void CreateCar() {
     Transform carTransform = Network.Instantiate(CarPrefab, position, rotation, 0) as Transform;
     carTransform.networkView.RPC("EnableDriving", Network.connections[0]);
 }

}

This code is all run on the server. My best guess is that the scene prefix for the Instantiate command is the old scene, and thus the instantiate never fully succeeds. If I have a network view not destroy on load, is there something I need to do to make it's network view get a new id when I load a new scene?

Thanks!

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 Azound · Nov 02, 2010 at 06:48 AM 0
Share

I tried adding the CreateCar logic to OnLevelWasLoaded, and that seems to work, though this code gets called on both the server and client. That seems to indicate that I'm just instantiating the cars too early. Is there something specific that I can have the server code wait for?

3 Replies

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

Answer by Leepo 1 · Nov 18, 2010 at 02:06 PM

Check the order of the calls. Maybe the "LoadLevel" RPC is send only AFTER the CreateCar call, in which case the Car would be deleted after loading the new level. Maybe it's not being executed at all because of the Application.LoadLevel("mylevel"); being called first.

Furthermore, this doesn't seem right to me: carTransform.networkView.RPC("EnableDriving", Network.connections[0]); I think this does not proberly work on the server.

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 anomalous_underdog · Feb 03, 2011 at 02:03 PM 0
Share

I had a similar problem with network objects instantiated after a scene load. Easiest is to make the car have DontDestroyOnLoad(gameObject), so it will still appear in the new scene. Of course, you need to keep track of the cars and delete them manually when needed, as they won't automatically be deleted anymore (There's no way to undo the applying of DontDestoryOnLoad property).

avatar image
2

Answer by Azound · Nov 19, 2010 at 07:39 AM

I had believed that level loading was a synchronous call, but in fact it sets a flag to load the level once it finishes the main loop. This meant that my calls to create the object were actually occurring before the new level was loaded (and then immediately destroyed). The proper thing to do is to Load the level, and then put the car creation code (or any code that's supposed to occur after the level load) in OnLevelWasLoaded.

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
avatar image
0

Answer by mcwind · Mar 31, 2011 at 03:01 AM

I met the same question and solved it at last. You may check out This thread for detail. Enjoy.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unity networking tutorial? 6 Answers

Remote database for leaderboard 2 Answers

PhotonNetwork does not run on Linux - Any alternatives 0 Answers

NetworkManager.StartClient not working in Unity release 2018.2.0f2 1 Answer

Network Error 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