Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Refresh spline doesn't seem to be working
hi, So I'm creating a spline and adding 2 control points, when I try to get a refernce to one of them I get a null ref exception, see code below :
The following code is run ONLY ONCE while the  left mouse button is down :
drawmousePOS_OBJ = GameObject.Find ("mousePOS");

drawmousePOS_OBJ.AddComponent<CurvySpline> ().Interpolation = CurvyInterpolation.Bezier;
drawing_spline = drawmousePOS_OBJ.GetComponent<CurvySpline> ();
drawing_spline.Add ().transform.position = v3;
drawing_spline.Add ().transform.position = v3;

drawing_spline.SetDirtyAll ();
drawing_spline.Refresh ();

drawtaxiCP0002 = GameObject.Find ("mousePOS/CP0001");
Debug.Log (drawtaxiCP0002.transform.position ); // this line creates a null ref exception

the spline & 2 control points are definately there.

But if i change the code so that 
drawtaxiCP0002 = GameObject.Find ("mousePOS/CP0001");

Debug.Log (drawtaxiCP0002.transform.position ); // works as expected !!

is ran  after the first time the mouse button is down it works.

It even though I'm refreshing the spline it seems like it wont refresh until update is run again.

As a general rule, the GameObject.Find method is an expensive one that should be avoided. In your case, you can avoid using it by doing this:

drawtaxiCP0002 = drawing_spline.Add ();
drawtaxiCP0002 .transform.position = v3;

instead of this:

drawing_spline.Add ().transform.position = v3;
drawtaxiCP0002 = GameObject.Find ("mousePOS/CP0001");

The reason why you had the null reference in the first place is probably because the newly created control point does not get renamed to CP0001 until the next editor update. We can go further in the path of the reasons and solutions regarding that point, but I think there is no need since there is the solution I gave above that avoid calling the Find method altogether.

If my solution does not work please let me know

Have a nice day
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Thanks ill try that.

Possibly Related Threads...
Thread Author Replies Views Last Post
  Same Spline Extrusion with variable width MarioBag 5 631 11-04-2019, 01:56 PM
Last Post: _Aka_
  Change curvy gizmos to show direction of spline Horibe 1 36 11-01-2019, 07:02 PM
Last Post: _Aka_
  Deform mesh on the spline Nico_CL 3 159 10-09-2019, 04:10 PM
Last Post: _Aka_
  Best practice for mass-object instancing along 100km spline (railways) plus streaming 10 674 09-24-2019, 03:42 PM
Last Post: _Aka_

Forum Jump: