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
  Curvy Line Renderer for UI Spline? gekido 1 1 6 hours ago
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 2 6 hours ago
Last Post: _Aka_
  Adjust radius of generated mesh via script? Shackman 1 3 03-26-2024, 01:12 PM
Last Post: _Aka_
Bug Changing spline connection in inspector causes splines to revert to defaults lacota 3 6 03-18-2024, 07:55 PM
Last Post: _Aka_

Forum Jump: