Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by knocker65 · Nov 20, 2013 at 02:03 AM · arrays

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;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Nov 20, 2013 at 02:12 AM

This:

 public Spline[] splineArray;

Is roughly equivalent to:

 public List<Spline> splineArray;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image knocker65 · Nov 20, 2013 at 04:01 AM 0
Share

Thank you rutter, this works great…Syntax!! It will be the death of me :-)

avatar image Bunny83 · Nov 20, 2013 at 04:35 AM 0
Share

@knocker65:
I guess you mean a compiler error because an assert is something else ;)

Ter$$anonymous$$ology!! :D

avatar image knocker65 · Nov 20, 2013 at 05:20 AM 1
Share

Syntax! Ter$$anonymous$$ology!! Seems I have one foot in the grave already ;-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges