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
0
Question by DayyanSisson · Jun 30, 2012 at 01:19 AM · inspectorarrayswaypointspathsjagged

Writing Jagged Arrays

For one of my scripts, the camera needs to follow a path with several different waypoints per path. So if there are 10 paths, then it needs to choose which path to take, then follow the waypoints of that path. So to hold all that information, I need to use jagged arrays. Although it takes up memory (as I've heard), it saves about a hundred lines of code per script (without the jagged array I would need to write an if statement for each of the paths and more paths are added as the game goes along so I need to use arrays to resize them). How do I write jagged arrays. I've tried this:

 public Transform[][] steps = new Transform[10][];

I'm assuming the first set of brackets is the number of arrays in the overall jagged array. But with that code, the variable doesn't show up in the Inspector. Is that how its supposed to be, or am I writing something wrong? If it's intended to be like that, how can I make it visible in the Inspector? I've already tried [ExecuteInEditMode]. What am I doing wrong here? Thanks in advace.

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
4
Best Answer

Answer by Bunny83 · Jun 30, 2012 at 01:43 AM

Jagged arrays as well as multidimensional arrays can't be serialized by Unity, so they can't be saved and don't show up in the inspector. However, there's an easy workaround and in your case it's even the better way ;)

Just create a container class for a single path and then create an array of those:

 // C#
 // MyWaypointScript.cs
 using UnityEngine;
 
 [System.Serializable]
 public class WaypointPath
 {
     public string name;
     public Transform[] points;
 }
 
 public class MyWaypointScript : MonoBehaviour
 {
     public WaypointPath[] paths;
 }

That's the result:

JaggedArray

Btw, you don't need to create the array since if the variable is serializable, Unity creates it automatically.

edit sorry, forgot the [System.Serializable] attribute ^^

Comment
Add comment · Show 6 · 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 Bunny83 · Jun 30, 2012 at 01:45 AM 0
Share

btw, the name variable has a nice side effect. Usually the array elements are called Element0, Element1 ... in the inspector. If the element is a serializable class, Unity will show the first string it founds in the class as name.

avatar image DayyanSisson · Jun 30, 2012 at 04:29 PM 0
Share

So do I create another script called WaypointPath, attach that to the gameObject and assign the points there, then access WaypointPath from the other script? I did that first and it worked but I had to make it a $$anonymous$$onobehaviour otherwise it gave me an error and then the name string being the element name didn't work.

avatar image DayyanSisson · Jun 30, 2012 at 04:48 PM 0
Share

Wait, scratch that I didn't need to make it a $$anonymous$$onobehaviour, but the array element names feature still doesn't work. The variable "name" is greyed out so I can't access it. Having that feature isn't essential but it is nice :D

avatar image Bunny83 · Jul 01, 2012 at 12:14 AM 0
Share

exactly, it's just a "normal" class that is derived from System.Object by default. It also doesn't need to be placed in another file. It can be in the same file as you $$anonymous$$onoBehaviour script. Only $$anonymous$$onoBehaviour or ScriptableObject derived classes have to be in a file that is named like the class, otherwise you can't attach them as components.

grayed out? if it's a public field it should be editable like any other variable.

I will edit the answer and make the example a bit more complete, just in case ;)

avatar image DayyanSisson · Jul 01, 2012 at 06:32 PM 0
Share

Oh! Alright that answers my question! Thank you. Yeah no idea why it was greyed out, it was a public field but it isn't anymore.

Show more comments

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

6 People are following this question.

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

Related Questions

Implementing a 2D array as a field on a ScriptableObject? 1 Answer

Inspector overring array. 1 Answer

Custom Construct Arrays in the Editor - can I control the name of the element? 0 Answers

When attempting to serialize and save a jagged array it gives me an error message 1 Answer

Adding variables to inspector strings 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