Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to access existing spline from custom script
#1
Hi,
I am new to Curvy and need help in accessing one existing CurvySpline from a custom script. 
So, I have created one CurvySpline name "XYZ". And then created one cube and attached the below script to this cube. When I run the scene, I expect the cube to move along the spline. 
In the below script, I dragged the curvy gameObject into the public variable "spline". But I am getting the error "NullReferenceException: Object reference not set to an instance of an object
TesterScript+<Start>c__Iterator35.MoveNext () (at Assets/Scripts/TesterScript.cs:11)



However, In the editor if I just add the SplineController script to the cube, then it starts moving along the spline without any issues. So, not sure what am I doing wrong. 
Can you please help me !!!



using UnityEngine;
using System.Collections;
using FluffyUnderware.DevTools;

public class TesterScript : FluffyUnderware.Curvy.Controllers.SplineController
{
    public FluffyUnderware.Curvy.CurvySpline spline;
    // Use this for initialization
    IEnumerator Start()
    {
            while (!spline.IsInitialized)
                yield return 0;
    }
    void Update () {
        if (spline && spline.IsInitialized)
        {
            Debug.Log(spline.name);
            Spline = spline;
            Speed = 20;
        }
    }
}


Regards,
Siloni
Reply
#2
Well actually I was trying to move a cube along a path through a script. Since, my script above was getting the error, I was not able to move it. Now, I did the following and its working:
1. Created the Spline in the editor.
2. Created a cube.
3. Attached the testerScript(Custom Script) and the SplineController.cs(Curvy Script) to the cube.
4. Play the Game.

testerScript(Custom Script)
----------------------------------------------------------------------

using UnityEngine;
using System.Collections;
using FluffyUnderware.DevTools;
using FluffyUnderware.Curvy.Controllers;
using FluffyUnderware.Curvy;

public class TesterScript : MonoBehaviour
{
public GameObject spline;
CurvySpline cSpline;
SplineController sController;
// Use this for initialization

void Start() {

cSpline = spline.GetComponent<FluffyUnderware.Curvy.CurvySpline>();
if (cSpline != null)
{
sController = GetComponent<SplineController>();
sController.Spline = cSpline;
sController.Speed = 20;

}
}
void Update() {

if (sController.IsInitialized) {
sController.Play();
sController.Refresh();
}
if (spline == null) { }
else
{


}
}
}


Thanks,
Siloni
Reply
#3
The error in the first script makes me assume spline was null the time you access it in Start(). Also, if you inherit a class from SplineController you don't need a Start() and an additional spline variable. Use the Spline property instead and override some of the methods used by the controller to add your custom behaviour. See here for more details on extending.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Wheel Collider to propel along Spline Phunyun 1 21 08-23-2019, 10:09 PM
Last Post: _Aka_
  Curvy Spline mover is jerky BlankMauser 5 87 08-16-2019, 09:31 PM
Last Post: _Aka_
  Camera wobbles on too long spline (100km+) Vectorvision 10 315 08-01-2019, 12:35 PM
Last Post: _Aka_
  Closest point From a Ray (a line), to the spline UsernameHed 5 190 07-31-2019, 03:57 PM
Last Post: _Aka_

Forum Jump: