Hi guys. I’m having real trouble with this for the last few days, and thought I would ask you brilliant people. Basically, I’m making an app were two people can draw over the network in real time, and see what others have drawn. To do this, I have a game object with a line renderer component I’m adding at runtime. I understand to spawn an object using UNET, it needs to be a prefab, and registered. What I’m wondering is, if I dynamically create a new game object and add a linerenderer, and add vertices, is there a way to spawn that game object?
I have this code here, but it’s not working. lineGO is the prefab which is pretty much an empty game object.
public void CreateLine() {
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
int id = touch.fingerId;
Vector3 m_Position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 2.0f);
GameObject temp_lineGO = Instantiate(lineGO, camera.ScreenToWorldPoint (m_Position), Quaternion.identity);
if (touch.phase == TouchPhase.Began) {
temp_lineGO.AddComponent<LineRenderer> ();
lineRenderer = temp_lineGO.GetComponent<LineRenderer> ();
lineRenderer.SetColors (c1, c2);
lineRenderer.startWidth = .1f;
lineRenderer.endWidth = .1f;
lineRenderer.SetVertexCount (0);
}
if (touch.phase == TouchPhase.Moved) {
lineRenderer.SetVertexCount (i + 1);
Vector3 mPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 2.0f);
lineRenderer.SetPosition (i, camera.ScreenToWorldPoint (mPosition));
i++;
}
if (touch.phase == TouchPhase.Ended) {
i = 0;
if(isLocalPlayer) {
CmdSpawn(temp_lineGO);
}
lines.Add (temp_lineGO);
}
}
}
[Command]
void CmdSpawn(GameObject line)
{
NetworkServer.Spawn (line);
}