- Home /
Question by
bathurst42 · Nov 24, 2018 at 08:09 PM ·
unity 2dgraph
How do I make a line graph with a line renderer in Unity? Or is there a better way?
The nodes instantiate just fine, but I just can't get the lines to render and connect the nodes. I tried to store the node transforms in a list and use that with LineRenderer.SetPosition, but that's clearly not working. I just want a simple representation of a line graph, nothing fancy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StockValueController : MonoBehaviour {
public Text valueDisplay;
private Button nextDayButton;
private float dayNumber = 0f;
private float xDistBetween = 2f;
private float distance;
public GameObject graphNode;
private Transform origin;
private Transform destination;
private List<Transform> nodeList = new List<Transform>();
public LineRenderer lineRenderer;
void Start () {
LineRenderer lineRenderer = GetComponent<LineRenderer>();
}
public void GenerateRandom()
{
//Generate random number within range, instantiate graph nodes and connections between them
distance = transform.position.x + (xDistBetween * dayNumber) - 12;
Vector3 position = new Vector3(distance, Random.Range(-2, 5), 1);
Instantiate(graphNode, position, Quaternion.identity);
nodeList.Add(graphNode.transform);
foreach (Transform t in nodeList)
{
origin = nodeList[nodeList.Count - 2];
destination = nodeList[nodeList.Count - 1];
}
Vector3 pointA = origin.position;
Vector3 pointB = destination.position;
lineRenderer.SetPosition(0, pointA);
lineRenderer.SetPosition(1, pointB);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612182712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sort VFX Graph Particles - Youngest in front 2 Answers
How to set up ZedGraph 0 Answers
Looking for a simple graph maker in Unity 0 Answers
Shader Graph Increment vector1 1 Answer