Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Ignacio 1 · Apr 07, 2011 at 07:25 PM · instantiateprefabruntimenetworkview

Adding a NetworkView component at runtime

Hi! I'm trying to add a NetworkView component to an object at runtime. Let me give you some background first: I'm trying to share a camera's transform across the network. In order to do that, I created a prefab that contains a camera (and which doesn't own a NetworkView component), instantiate it with Network.Instantiate and add a NetworkView component at runtime. Here's the code:

Network.Instantiate(AstronautCamera, AstronautCamera.transform.position, AstronautCamera.transform.rotation, 0);
GameObject AstronautCameraInstance = GameObject.FindWithTag("AstronautCamera");
AstronautCameraInstance.AddComponent(typeof(NetworkView));
AstronautCameraInstance.networkView.observed = AstronautCameraInstance.transform;
AstronautCameraInstance.networkView.stateSynchronization = NetworkStateSynchronization.ReliableDeltaCompressed;
NetworkViewID viewID = Network.AllocateViewID();
AstronautCameraInstance.networkView.viewID = viewID;

The code above compiles and executes fine, but it's not doing what I expect it to do (i.e., when running in networked mode, the camera's transform doesn't get transferred across to the other hosts). Notice that assigning a NetworkView component to the prefab in the editor and instantiating the prefab using Network.Instantiate works fine, but I really need to do it by code.

Do you have any suggestions? Thanks in advance for your input!

--Nacho

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

3 Replies

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

Answer by Ignacio 1 · Apr 08, 2011 at 02:35 PM

I finally solved it! The problem was that I wasn't adding the NetworkView component inside an RPC. I made a few changes and now everything's working OK.

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
3

Answer by AjinkyaKshirsagar · Oct 16, 2013 at 03:08 AM

Could you please post the solution code ? Thanks !

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
1

Answer by emilhauk · Apr 29, 2014 at 08:59 PM

@AjinkyaKshirsagar: Late, but someone may need this.

This would be a way to attach a NetworkView runtime

 public void Awake()
 {
     AddNetworkView();
 }
 
 [RPC]
 private void AddNetworkView()
 {
     gameObject.AddComponent<NetworkView>();
     gameObject.networkView.observed = this;
     gameObject.networkView.stateSynchronization = NetworkStateSynchronization.ReliableDeltaCompressed;
     gameObject.networkView.viewID = Network.AllocateViewID();
 }

The trick is to add [RPC]. This will add the network view to the queue of messages to be shared with other players.

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 petrbena · Oct 06, 2014 at 11:02 AM 0
Share

This doesn't really work, if you really wanted to do this runtime and not only during Awake, but somewhere else, it wouldn't happen on all clients, only that one you call it on, thus network view would not be operational. Only solution that works is to create network view always, but disable it when you don't need it

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

2 People are following this question.

avatar image avatar image

Related Questions

How to instantiate a Prefab ? 2 Answers

Adding behaviors to a game object at run time via scriptable object 1 Answer

nee help with Instantiating prefab at mouse position with GUI button 0 Answers

Instantiate a prefab at runtime 2 Answers

Put a prefab in Hierarchy without using Instantiate ? (from code) 2 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