- Home /
UIExtensions - LineRenderer not updating
Hi, I'm using the UILineRenderer from the UI extensions asset to plot something. I'm periodically updating the set of points in the LineRenderer however the line doesn't seem to change in Play mode even if I see the points changing. If I try to toggle one of the object options the line gets updated so I think there's a rendering issue here. Here's an example of the code:
using UnityEngine.UI.Extensions;
using UnityEngine.UI;
void Start(){
StartCoroutine("coroutine");
}
public class Plot : MonoBehaviour {
Vector2[] linePoints;
int pTime;
IEnumerator coroutine(){
Debug.Log("Start service");
while(true){
pTime = Random.Range(-50,50);
updateLine();
yield return new WaitForSeconds(1);
}
}
void updateLine(){
Debug.Log("Updating");
int i;
for(i = 0; i < numPoints-1; i++)
linePoints[i].y = linePoints[i+1].y;
linePoints[i].y = pTime;
lineRenderer.Points = linePoints;
}
}
I have no fix for you but other things in your code "smell" a bit. Let me unpack those in comments:
public class Plot : $$anonymous$$onoBehaviour
{
Vector2[] linePoints;
void Start ()
{
StartCoroutine( LineRendererRoutine() );//do not use string argument here (slower, bug-prone, redundant)
}
IEnumerator LineRendererRoutine ()
{
var second = new WaitForSeconds(1);//creating this small object every frame is a kind of memory leak; it will introduce ugly cpu spikes (GC)
while( true )
{
#if DEBUG
if( linePoints.Length==0 ) { Debug.LogWarning( "linePoints contains nothing" , gameObject ); }
#endif
//update line renderer:
{
int pTime = Random.Range( -50 , 50 );
int numPoints = linePoints.Length;
for( int i = 0 ; i<numPoints-1 ; i++ )
{
//get data from array:
Vector3 p0 = linePoints[ i ];
Vector3 p1 = linePoints[ i+1 ];
//calculate new point
Vector3 p0New = p0;
p0New.y = p1.y;
p0New.y = pTime;//...but you changed 'y' line above already https://i.imgur.com/1vZr6hs.jpg
//update data in array:
linePoints[ i ] = p0New;
}
//PROTIP: skip "{}" brackets when using 'for' loop to introduce silly bugs
//update line renderer when array when job is done:
lineRenderer.Points = linePoints;
}
//await:
yield return second;
}
}
}
Hi @andrew-lukasik, thanks for your answer. $$anonymous$$y problem is not that the points in the array are not changing, but the fact that the new points are not rendered. I can see from the Inspector that the values of my points are changing, however they remain the same on the screen unless I modify something of the UI Line Renderer from the Inspector (e.g. toggle Use Native Size
).
Answer by AGaballo · Jan 08, 2019 at 10:48 AM
Solved by calling lineRenderer.SetAllDirty()
after updating the points.