Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create splines at runtime: correct initialization
#1
Hi, I want to create splines at runtime with the follow code:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FluffyUnderware.Curvy;
using FluffyUnderware.Curvy.Controllers;

public class SplineCreatorTest : MonoBehaviour{
   private bool keyUp = true;
   void Update(){
       if (Input.GetKey(KeyCode.M) & keyUp){
           keyUp = false;
           gameObject.AddComponent<CurvySpline>();
           gameObject.GetComponent<CurvySpline>().Interpolation = CurvyInterpolation.Bezier;
           List<GameObject> goList = new List<GameObject>();
           for (int i = 0; i < 7; i++){
               goList.Add(new GameObject("CP000" + i.ToString()));
               goList[i].transform.SetParent(this.transform);
               goList[i].gameObject.AddComponent<CurvySplineSegment>();
           }
           goList[0].transform.localPosition = new Vector3(0.0f,0.0f,0.0f);
           goList[1].transform.localPosition = new Vector3(0.0f, 0.0f, 156.0f);
           goList[2].transform.localPosition = new Vector3(100.0f, 0.0f, 256.0f);
           goList[3].transform.localPosition = new Vector3(200.0f, 0.0f, 156.0f);
           goList[4].transform.localPosition = new Vector3(100.0f, 0.0f, 56.0f);
           goList[5].transform.localPosition = new Vector3(0.0f, 0.0f, 156.0f);
           goList[6].transform.localPosition = new Vector3(0.0f, 0.0f, 256.0f);
           
           goList[1].gameObject.transform.GetComponent<CurvySplineSegment>().AutoHandles = false;
           //NullReferenceException: Object reference not set to an instance of an object
           //SplineCreatorTest.Update()(at Assets / SplineCreatorTest.cs:31)
           goList[1].GetComponent<CurvySplineSegment>().HandleIn = new Vector3(0.014761f, 0.0f, -54.69138f);
           goList[1].GetComponent<CurvySplineSegment>().HandleOut = new Vector3(-0.014761f, 0.0f, 54.69138f);
           
           goList[5].gameObject.transform.GetComponent<CurvySplineSegment>().AutoHandles = false;
           goList[5].GetComponent<CurvySplineSegment>().HandleIn = new Vector3(0.62f, 0.0f, -57.872f);
           goList[5].GetComponent<CurvySplineSegment>().HandleOut = new Vector3(-0.62f, 0.0f, 57.872f);
       }
       if (Input.GetKeyUp(KeyCode.M)) { keyUp = true; }
   }
}

But I receiving this error when "AutoHandles = false" runs:
Code:
NullReferenceException: Object reference not set to an instance of an object SplineCreatorTest.Update()(at Assets / SplineCreatorTest.cs:31)

But on the properties "AutoHandles" already is off.
If I wait next update and trying:
Code:
           goList[1].gameObject.transform.GetComponent<CurvySplineSegment>().AutoHandles = false;
           goList[1].GetComponent<CurvySplineSegment>().HandleIn = new Vector3(0.014761f, 0.0f, -54.69138f);
           goList[1].GetComponent<CurvySplineSegment>().HandleOut = new Vector3(-0.014761f, 0.0f, 54.69138f);
           
           goList[5].gameObject.transform.GetComponent<CurvySplineSegment>().AutoHandles = false;
           goList[5].GetComponent<CurvySplineSegment>().HandleIn = new Vector3(0.62f, 0.0f, -57.872f);
           goList[5].GetComponent<CurvySplineSegment>().HandleOut = new Vector3(-0.62f, 0.0f, 57.872f);
the scripts runs whitout erros.
Probably I'm missing something to correct initialization. What I need to do?
Reply
#2
Hi,
The correct way to add control points to a spline is by using the methods from the CurvySpline class, such as Add, InserrBefore, etc...
Is my answer clear?
Have a nice day
Please consider leaving a review for Curvy, this helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Reply
#3
(10-29-2019, 03:16 PM)_Aka_ Wrote: Hi,
The correct way to add control points to a spline is by using the methods from the CurvySpline class, such as Add, InserrBefore, etc...
Is my answer clear?
Have a nice day
Thanks, solved my problem.

Code:
gameObject.AddComponent<CurvySpline>();
CurvySpline c = gameObject.GetComponent<CurvySpline>();
c.Interpolation = CurvyInterpolation.Bezier;
c.Add(new Vector3(0.0f,0.0f,0.0f));
c.Add(new Vector3(0.0f, 0.0f, 156.0f));
c.Add(new Vector3(100.0f, 0.0f, 256.0f));
c.Add(new Vector3(200.0f, 0.0f, 156.0f));
c.Add(new Vector3(100.0f, 0.0f, 56.0f));
c.Add(new Vector3(0.0f, 0.0f, 156.0f));
c.Add(new Vector3(0.0f, 0.0f, 256.0f));

c[1].AutoHandles = false;
c[1].HandleIn = new Vector3(0.014761f, 0.0f, -54.69138f);
c[1].HandleOut = new Vector3(-0.014761f, 0.0f, 54.69138f);

c[5].AutoHandles = false;
c[5].HandleIn = new Vector3(0.62f, 0.0f, -57.872f);
c[5].HandleOut = new Vector3(-0.62f, 0.0f, 57.872f);
Reply
#4
You are welcome
Please consider leaving a review for Curvy, this helps immensely. Thank you.
Available for freelance work—feel free to reach out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information New free asset: Converter For Dreamteck Splines _Aka_ 0 0 07-19-2024, 08:27 PM
Last Post: _Aka_
Information New free asset: Converter For Unity Splines _Aka_ 20 61 07-19-2024, 05:15 PM
Last Post: _Aka_
  Invisible Gizmos In Latest Curvy Splines orrenravid1 1 5 06-11-2024, 07:36 AM
Last Post: _Aka_
  Twisting Splines ZiggZagg 6 15 05-14-2024, 04:48 PM
Last Post: _Aka_

Forum Jump: