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 littlemegzz · Nov 25, 2011 at 01:16 AM · arrayarrayselementeditingmultidimensional

Multidimensional arrays - editing one element within one of the arrays

I have a multidimentional array set like the following

 // 0 = G#, 1 = F#, 2 = E, 3 = D, 4 = C
 // Second element is the time variable
 songValues[0]  = [0,2.05];
 songValues[1]  = [4,5.93];
 songValues[2]  = [3,9.80];

I would like to be able to access the first element in one of the arrays (say songValues[0]) and change its value.

I already have a code which sends a message and variable to activate the following function (this function is within the same javascript as the above code)

 function EditNoteType(noteValue : int){
     Debug.Log("edit note type worked! " + noteValue);
 }

This works. Now I want to change the first variable in songValues[0] without changing the time variable.

(I'm not sure whether I can do this with some sort of "Shift/Pop" or not. My first attempt at this failed when I wrote:

 songValues[0].Shift;

)

Comment
Add comment · Show 4
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 Jessy · Nov 25, 2011 at 01:50 AM 1
Share

$$anonymous$$ultidimensional arrays are a bad choice. You should create a class or struct, ins$$anonymous$$d.

avatar image littlemegzz · Nov 25, 2011 at 02:03 AM 0
Share

what is a struct? Do you have a reference for creating a class or struct in javascript?

avatar image Jessy · Nov 25, 2011 at 02:39 AM 0
Share

I provided a suggestion below. Let me know if you have any questions about it.

avatar image sokmeng · Nov 25, 2011 at 04:29 AM 1
Share

What is the multidimensional array?Why is it a bad choice?

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Jessy · Nov 25, 2011 at 02:38 AM

If you search around, you'll find that Array is generally a terrible thing; use List instead: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

 #pragma strict
 import System.Collections.Generic;
 
 enum Note {c, d, e, fSharp, gSharp}
 
 class SongData {
     var note : Note;
     var time : float;
     
     function SongData(note : Note, time : float) {
         this.note = note;
         this.time = time;
     }
 } var songValues : List.<SongData>;
 
 function Reset() {
     songValues = new List.<SongData>([
         new SongData(Note.gSharp, 2.05),
         new SongData(Note.c, 5.93),
         new SongData(Note.d, 9.80)
     ]);
 }
Comment
Add comment · Show 13 · 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 jahroy · Nov 25, 2011 at 04:00 AM 0
Share

Using a class to represent song values is definitely the way to go...

avatar image littlemegzz · Nov 27, 2011 at 05:46 AM 0
Share

thanks for your help. I will use lists next time, but since my current assignment is due on Tuesday, I will manage for the time being.

avatar image ina · Dec 18, 2011 at 03:33 AM 0
Share

are there limitations to lists? ie things you can usually do with arrays that can't be done with lists?

avatar image ina · Dec 18, 2011 at 03:39 AM 0
Share

mmm i get this error: Can't add script behavior SongData. The script needs to derive from $$anonymous$$onoBehavior!

avatar image ina · Dec 18, 2011 at 03:42 AM 0
Share

@Jessy - also, there is a bit about how you should not use constructors to initialize values. http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html

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

Multidimensional array trouble 2 Answers

Using class arrays for multiple Variables per element also Naming Elements 2 Answers

Distance error: Cannot cast from source type to destination 0 Answers

How to return index of List element? 1 Answer

How many audio clip arrays are supported? 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