Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best practice when Instantiating game object with spline
#1
Hi, I create splines many times using a prefab which is a simple GameObject with a Curvy Spline component. I create it like this:

Code:
splineClone = GameObject.Instantiate(splinePrefab, new Vector3(), Quaternion.identity);

Immediately after this, I grab the spline component and set properties on it, including the actual spline points:

Code:
spline = splineClone.GetComponent<CurvySpline>();
spline.Orientation = CurvyOrientation.Static;
CurvySplineSegment[] cps = spline.Add(pathPoints.ToArray());

However, should I be checking spline.IsInitialized first before fiddling with the spline? In other words, do I need to be waiting for some form of initialisation in the CurvySpline componenrt before I set it's control points, even though it doesn't have any control points to start with?
In addition, should I be calling Refresh after adding the control points or before?

As a general rule of thumb when doing everything via the API, when should I be checking the IsInitialized property and calling Refresh?

Thank you
Reply
#2
Hi,
CurvySpline.IsInitialized is set to true at OnEnable and Start. When instantiating via prefab, one of those two methods is not called, or even both of them, I don't remember*. If you don't bother doing tests or looking on internet, the best is to call them both, OnEnable then Start, before using the spline. "Overcalling" those methods should not create any issue.

*The script initialization process in Unity is a hell. It's different depending on Unity's version, on whether you instantiated via editor or script, via prefab or not, on whether it is an editor script or a runtime script, ... Each time I learn it for the sake of debugging or implementing a feature, I forget about it soon after. And of course, the official Unity documentation is incomplete.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Kinematic object on spline Dayus 3 47 11-23-2020, 07:38 PM
Last Post: _Aka_
  Getting MissingReferenceException from the component pool when I destroy a spline Excrubulent 5 111 11-22-2020, 11:38 AM
Last Post: _Aka_
  Generate Mesh Spline from GameObject processor 4 282 10-01-2020, 11:00 AM
Last Post: _Aka_
  Instantiating prefab Abnormalia 3 356 08-28-2020, 02:11 PM
Last Post: _Aka_

Forum Jump: