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 freefalll · Jul 19, 2012 at 10:03 AM · 2darraygrid2d array

2d Array persists during multiple game sessions

I'm in the very early stages of building up a grid based rts game. For some reason the 2d array I'm creating (consisting of a custom object/script called GridCell) remains the same no matter how I change the values that determine the coordinates (sizeGridX and sizeGridY). The coordinates simply don't change after I've attached the script and ran the game once. Even if I multiply the sizeGridX by 10, it doesn't update unless I re-attach the script.

Is there any way that I can create a new array each time I start the game?

 public var totalGridX : int = 10; 
 public var totalGridY : int = 10; 
 
 public var sizeGridX : int = 100;
 public var sizeGridY : int = 100;
 
 var tilePrefab : Transform;
 var gridArray : GridCell[,] = new GridCell[10,10];
 
 var mainCamera : GameObject;
 //var nameN : int = 0;
 
 var gridPlane : Transform;
 
 function Start() {
 // gridArray = GridCell[totalGridX, totalGridY];
  
  for(var x : int = 0; x < totalGridX; x++){
 
         for( var y : int = 0; y < totalGridY; y++){
         
         var gc : GridCell = GridCell();
         gc.CordX = (sizeGridX / 2) + (sizeGridX * x) ;
         gc.CordY = (sizeGridY / 2) + (sizeGridY * y) ;
         print(gc.CordX);
         print(gc.CordY);
  
  gridArray[x,y] = gc;
 
         }
     }
     
     displayGrid();
 }
 
 
 
 function Update() {
 
 }
 
 
 function displayGrid() {
 Debug.Log("test");
  for(var x : int = 0; x < totalGridX; x++){
         for( var y : int = 0; y < totalGridY; y++){
         var gc : GridCell = gridArray[x,y];
         print(gc.CordX);
         print(gc.CordY);
 
          Instantiate (gridPlane, Vector3(gc.CordX, 0, gc.CordY), Quaternion.Euler(-90, 0, 0));
         print("Made it!");
 
         }
         
     }
 }
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

2 Replies

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

Answer by Fattie · Jul 19, 2012 at 01:35 PM

Just add

@System.NonSerialized

on the line immediately before var gridArray. It's that easy.

Or! click the very little-known RESET button here:

alt text

I only learned this here on this here mailing list, so, now I am happy to tell someone else! Good luck


resetmystery.jpg (22.3 kB)
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 freefalll · Jul 19, 2012 at 03:16 PM 0
Share

Thanks a ton :) Now ins$$anonymous$$d of having this unsolvable problem, I end up with three different solutions!

avatar image freefalll · Jul 19, 2012 at 04:09 PM 0
Share

Done, I still have one question though. As I wrote as a comment to Seth's answer: when I change the gridArray to

gridArray = new GridCell[totalGridX,totalGridY];

I still get a warning that: "You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed."

Is there anyway around this?

avatar image Fattie · Jul 19, 2012 at 04:26 PM 0
Share

(1) I would EXTRE$$anonymous$$ELY STRONGLY URGE YOU to NOT use plain arrays and in fact you should use List. there are only very very rare specific situations, today, where you should ever use plain arrays. Have faith and use List.

(2) regarding your specific question, does this help http://answers.unity3d.com/questions/54695/how-to-declare-and-initialize-multidimensional-arr.html

As you know, this works fine ...

 var gridArray : int[,];
 then...
 gridArray = new int[5,5];

so there must be something I don't understand about GridCell. Hope it helps!

avatar image
0

Answer by Seth-Bergman · Jul 19, 2012 at 10:19 AM

Once you've attached the script, all subsequent changes to the variables must be made in the INSPECTOR panel, changing it in the script won't do anything. select the object in the scene containing the script, and look for the script in the inspector panel

EDIT:

Oh, and maybe add in the top of the Start function:

gridArray = new GridCell[totalGridX,totalGridY];

Comment
Add comment · Show 1 · 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 freefalll · Jul 19, 2012 at 03:15 PM 0
Share

Thanks so much, I'd been looking for hours and figured that I created the array incorrectly. That definitely solves it though :)

One more question thoughwhen I change the gridArray as per your suggestion (and like I used to have it), I still get a warning that: "You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed."

Is there anyway around this?

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

2D Grid Question 0 Answers

Issue with instantiating multiple clone GameObjects 1 Answer

Creating a 2d array with the Array class 1 Answer

Tracking ojects grid positon 1 Answer

2D array generated with custom inspector, null when starting game? 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