- Home /
Convert BuiltIn array to an Generic List?
OK this one has me stumped.
I have a game object with the script below attached. The script works fine, It provides a BuiltIn array list of splines in the inspector window and on runtime the gameObject will travel along a spline and when it reaches the end of the spline, pick another random spline in the array list and travel along that one etc.
What I want to do is to declare a Generic List instead of a built in array so I can then Add() and RemoveAt() splines within the array at runtime. I know this can't be done with a built in and am struggling to find the correct syntax to do so.
I know it would be something like..
public List <GameObject> splineArray = new List <GameObject>();
…but I also need to include the type Spline in there somewhere. Maybe this is the wrong way completely and I need to convert from builtin to a list at runtime and then back again, as I said I am stumped :-)
Any help/ideas/direction would be really appreciated.
Thanks.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // Added here for the Generic List
//This class animates a gameobject along the spline at a specific speed.
[AddComponentMenu("SuperSplines/Animation/Regular Animator")] public class SplineAnimator : MonoBehaviour {
public Spline[] splineArray;
private Spline spline;
public GameObject prefab;
public WrapMode wrapMode = WrapMode.Clamp;
public GameObject go;
public float speed = 1f;
public float offSet = 0f;
static float passedTime = 0f;
void Start() {
spline = splineArray[Random.Range (0, splineArray.Length )];
}
void Update( )
{
passedTime += Time.deltaTime * speed;
float clampedParam = WrapValue( passedTime + offSet, 0f, 1f, wrapMode );
//Interpolate a custom value according to the interpolation type and spline settings
//The custom values can be set in the SplineNode scripts or in the inspector
float customValue = spline.GetCustomValueOnSpline( clampedParam );
transform.position = spline.GetPositionOnSpline( clampedParam );
transform.rotation = spline.GetOrientationOnSpline( clampedParam );
go.renderer.material.color = Color.white * (1f-customValue) + Color.black * (customValue);
if (passedTime > 1f) {
passedTime = 0f;
// complete a spline and then pick a new one from array
spline = splineArray[Random.Range (0, splineArray.Length )];
}
}
private float WrapValue( float v, float start, float end, WrapMode wMode )
{
switch( wMode )
{
case WrapMode.Clamp:
case WrapMode.ClampForever:
return Mathf.Clamp( v, start, end );
case WrapMode.Default:
case WrapMode.Loop:
return Mathf.Repeat( v, end - start ) + start;
case WrapMode.PingPong:
return Mathf.PingPong( v, end - start ) + start;
default:
return v;
}
}
}
Answer by rutter · Nov 20, 2013 at 02:12 AM
This:
public Spline[] splineArray;
Is roughly equivalent to:
public List<Spline> splineArray;
Thank you rutter, this works great…Syntax!! It will be the death of me :-)
@knocker65:
I guess you mean a compiler error because an assert is something else ;)
Ter$$anonymous$$ology!! :D
Syntax! Ter$$anonymous$$ology!! Seems I have one foot in the grave already ;-)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problem with array on gameobjects 2 Answers
How can I code a pattern detector in an array of integers? 1 Answer
I have never seen a null reference exception like this one! 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer