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
0
Question by Swecha · Jul 10, 2014 at 07:30 AM · networklinerenderer

Instantiate LineRenderer on the network

I am trying to instantiate a linerenderer object on network.instantiate but it only works on the server side but not on the client side. My code looks something like this:

 var edgesL : LineRenderer;
 
 edgesL.SetPosition(0, startPosition);
 edgesL.SetPosition(1, endPosition);
         
 edgesL.tag = "edge1";
 edgesL.name = "edge1";
 
 Network.Instantiate(edgesL, Vector3.zero, Quaternion.identity, 0);

It works when I do the same for a gameObject (i.e. it appears on both client and server side) but not for a lineRenderer. The setposition values do not get updated on the client side. I have created a prefab ( and set it to edgesL on the Editor) and attached a NetworkView component also. Please help. I am a beginner in Unity and this is my first attempt at a multilayer game. I have attached an image as wellalt text

Thank you

attachedgame.jpg (110.7 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 Tehnique · Jul 10, 2014 at 07:54 AM

Try to add:

  networkView.RPC("DrawLine", RPCMode.AllBuffered, startPosition, endPosition);

And add a "DrawLine" RPC method that takes the 2 positions and draws the line:

 [RPC]
 public void DrawLine(Vector3 startPos, Vector3 endPos) 
 {
 ...
 }
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 Swecha · Jul 10, 2014 at 08:03 AM 0
Share

Hi, I have tried that. But it didn't work. Thank you for your response.

avatar image Tehnique · Jul 10, 2014 at 08:17 AM 0
Share

Hmmm, from what I understand, you have to make an object that contains the line renderer add a network view to that object, make that object a prefab, and instantiate it on the network.

That object should have an RPC method for setting the positions and drawing the lines. On the server you would just call the RPC method.

So I think that it is important to be careful what you added your network view to: the object that contains the line renderer or the line renderer itself? Because I don't think it will work if you try to add it to the line renderer, as you cannot flag it's methods as RPC.

These are just guesses based on what I've read about the components, I cannot access Unity right now.

avatar image Swecha · Jul 18, 2014 at 05:49 AM 0
Share

Hi, sorry for the late reply. I have an object that contains the line renderer to which I have added the network view component and yes the object is a prefab, but it still does not instantiate on the network. And, when you say "RPC method for setting positions and drawing the lines" what do you mean by drawing the lines? For linerenderer , setting the position and instatiating is enough to "draw" the line, or do I need to do something else? Can you please clarify? Thank you.

avatar image Tehnique · Jul 18, 2014 at 06:33 AM 0
Share

I mean just that: a method that sets the new positions (implicitly drawing the new set of lines). From your initial question I understand that you had the object working on the network, apart from the position setting step. Now what's the situation, the same?

avatar image Swecha · Jul 18, 2014 at 06:49 AM 0
Share

In the DrawLine RPC, I did as you mentioned, just set the new positions of the linerenderer but it still does not appear on the client side. From what I understand, it seems the linerenderer.setposition values are not updated on the network. The linerenderer.setposition values for all four linerenderer objects in my scene (on the client side) have the same random value (and not the values that they have on the server side). That's why they don't "appear" on the scene although they are actually instantiated. I hope you understand what I mean. Thank you.

Show more comments
avatar image
0

Answer by NathanWheeler · Mar 04, 2018 at 06:37 PM

I'm going to necro this thread, because I've been searching for an easy way to do this and finally came up with my own way that seems to work really well. Here's how I accomplished it. Just attach this to your LineRenderer object/prefab, then on the server, set the positions for the line using the SyncVars instead of setting the positions of the line itself.

 public class LineBehavior : NetworkBehaviour 
 {
     [SyncVar(hook = "SetPosition1")]
     public Vector3 Position1;
 
     [SyncVar(hook = "SetPosition2")]
     public Vector3 Position2;
 
     // Use this for initialization
     void Start () {
         SetPosition1(Position1);
         SetPosition2(Position2);
     }
 
     public void SetPosition1(Vector3 Position)
     {
         this.GetComponent<LineRenderer>().SetPosition(0, Position);
     }
 
     public void SetPosition2(Vector3 Position)
     {
         this.GetComponent<LineRenderer>().SetPosition(1, Position);
     }
 }

I assume you could make a multi-point line work instead using SyncListStruct, but I haven't tried it, so use at your own risk.

 public class LineBehavior : NetworkBehaviour 
 {
     [SyncVar(hook = "UpdatePositions")]
     public SyncListStruct<Vector3> Positions = new SyncListStruct<Vector3>();
     
     void Start()
     {
         UpdatePositions(Positions);
     }

     public void AddPosition(Vector3 Position)
     {
         Positions.Add(Position);
     }

     public void SetPosition(int Index, Vector3 Position)
     {
         Positions[Index] = Position;
     }
     
     public void UpdatePositions(SyncListStruct<Vector3> Positions)
     {
         LineRenderer line = this.GetComponent<LineRenderer>();
         
         for (int i = 0; i < Positions.Count; i++)
         {
             line.SetPosition(i, Positions[i]);
         }
     }
 }



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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How draw a path using touch in c# 1 Answer

Collision detection with Line renderer 1 Answer

Collider is not working... 2 Answers

headless mode only pro? 1 Answer

Photon Network Muzzleflash 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