- Home /
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 well
Thank you
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)
{
...
}
Hi, I have tried that. But it didn't work. Thank you for your response.
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.
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.
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?
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.
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]);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613153703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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