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 Nk.Andrei · Jul 11, 2013 at 05:50 PM · 2darrayclass

2d array.What is the difference between these two?

I have a simple class and create a 2d array to store objects in x,z coordonates:

var classArray : MyClass[,]; var classTest : MyClass

var x : int; var z : int;

function Update() { classArray = new MyClass[4,4]

for(x=0 ; x<4 ; x++) { for(z=0 ; z<4 ; z++) { classArray[x,z] = classTest;

 or                               ////what is the difference betwen these two

classTest = classArray[x,z];

} }

}

I would like to know what is the difference betwen those two examples and what effects do they have. Are these both the same or are these any different.

I came across this in a tutorial and would like to know more about it before I continue instead of blindly copy/pasting everything.

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

Answer by swyrazik · Jul 11, 2013 at 06:13 PM

In the first case the element of the classArray with indices x,z is given the value of the classTest, which is equal to null because it was never initialized, and in the end of the code execution the classArray will be filled with null elements.

In the second case the classTest variable will be continuously given the value of the element of the classArray with indices x,z, overriding its previous value, and in the end of the code execution the classTest variable will have the value of the last element of the classArray, i.e. a reference to the last MyClass instance of the array (classArray[3,3]).

If this wasn't clear enough, I could try to explain it more.

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 Nk.Andrei · Jul 12, 2013 at 10:58 AM 0
Share

If i understand correctly :

classArray[1,1] = classTest; classArray[1,2] = classTest; .......... classArray[4,4] = classTest;

but classTest = classArray[4,4];

Am I correct? And if I am in what circumstances could I use each of them? A simple examle would be nice.

avatar image swyrazik · Jul 12, 2013 at 01:01 PM 1
Share

Remember: arrays start from index 0! An array with 4 elements will have indices 0,1,2 and 3. So in this case you will have: classArray[0,0] = classTest; classArray[0,1] = classTest; ... classArray[3,3] = classTest;

or in the second case: classTest = classArray[3,3];

You will more often use the first case. For example, when you want to initialize or modify the value of all the elements of the array, like:

 for (x = 0; x < 4; x++){
     for (z = 0; z < 4; z++){
         classTest = new $$anonymous$$yClass();    //Create an instance of $$anonymous$$yClass
         classTest.SetId(x*4 + z);     //SetId is a function of $$anonymous$$yClass I came up with
         //make any additional modifications you want to classTest
         classArray[x,z] = classTest;  //The element x,z will now be equal to the instance of $$anonymous$$yClass you created, i.e. classTest
     }
 }

The second case, as you noticed, is equal to just writing classTest = classArray[3,3] therefore the loops are pointless.

avatar image Nk.Andrei · Jul 13, 2013 at 11:44 AM 0
Share

Tahnks :)

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

16 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

Related Questions

find prefabs that extend Base Class c# 0 Answers

Generic List - Class Add Dynamically 1 Answer

Why is my C# array of objects fully populated when some of them should be null? 3 Answers

Warning on creating an array of class objects 3 Answers

Problems with 2d arrays , += incrementing values 0 Answers


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