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 Alexrose · Mar 22, 2012 at 02:11 AM · c#arraysstaticpublicglobal

C# Global Dynamic Arrays

Hi, I'm new to C# and general OOP, and I'm having some difficulty understanding the dynamic arrays.

I have two scripts, one called GridDeclare and one called GridSpawn. It's necessary that they're separate scripts.

Within GridDeclare I want the ability to adjust the grid size, so within my GridDeclare class I've used:

 public int xsize = 10;
 public int zsize = 10;
 

Which can obviously be modified within the Object Inspector.

Then I want to introduce my grids themselves.

 public static int[,] grid;
 public static int[,] run;
 public static bool[,] runvalid;

However, I want them to be of size [xsize,zsize]. So within my Start() function I declared:

 int[,] grid = new int[xsize, zsize];
 int[,] run = new int[xsize, zsize];
 bool[,] runvalid = new bool[xsize, zsize];

I then used Streamreader to read a tab delimited text file into my grid array.

I then analysed the data to fill in my run and runvalid tables.

Next I wrote a bunch of scripts which, upon enabling, will are meant to read in the run and runvalid arrays and edit them, and then save them again.

But obviously I've initialised them as just standard variables within my Start function, so I just get a NullReferenceException: Object Reference not set to an instance of an object.

So I'm left with a dilemma, in that I want to create a global array that I can adjust the size of at runtime, access from other scripts and save to, and the only way I can think of doing it right now is a really sloppy workaround where'd I'd save the array data to a text file every time I modify it and read it in every time I access it. Any tips?

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

Answer by rutter · Mar 22, 2012 at 02:59 AM

Looks like you may have declared two sets of variables named grid, run, and runvalid.

Try changing your Start() function from this:

 int[,] grid = new int[xsize, zsize];
 int[,] run = new int[xsize, zsize];
 bool[,] runvalid = new bool[xsize, zsize];

To this:

 grid = new int[xsize, zsize];
 run = new int[xsize, zsize];
 runvalid = new bool[xsize, zsize];

Otherwise, you're actually creating an entirely new set of variables which unfortunately happen to have the same name, type, and general behavior... but which also effectively stop existing at the end of that one code block.

(Wikipedia's article on variable scope has a little more explanation, if you're curious.)

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 Alexrose · Mar 22, 2012 at 03:47 AM 0
Share

You can't do that. For a start you'd need to declare it as e.g.:

 int grid = new int[xsize,zsize];

But you can't, because xsize and zsize aren't static variables.

You can declare them within the function fine, but you can't declare them as public within the function, and they have to be public. xsize and zsize have to be static too. I can't see a way of doing this.

avatar image rutter · Mar 22, 2012 at 04:01 AM 2
Share

Are you responding to me? If so, please understand that declaration and assignment are not the same thing.

This compiles fine, for me:

 public class Foo
 {
     static int[,] grid;
     
     public int xsize = 5;
 
     void Start () 
     {
         grid = new int[xsize,xsize];
     }
 }

Are you having a specific problem or error?

avatar image Alexrose · Mar 22, 2012 at 05:39 AM 0
Share

I just understood what you meant, and I love you <3.

I was being an idiot; I thought you meant do that from outside the Start function for some reason. But yes, you've fully solved my problem, thanks a lot.

avatar image
1

Answer by DaveA · Mar 22, 2012 at 02:13 AM

Your answer may lie here: http://unity3d.com/support/documentation/ScriptReference/Array.html

Using this type of array, basically to resize it means copying it to another array with the new size (then back again I would presume).

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 sdgd · Jan 22, 2013 at 01:58 AM 0
Share

and for C#? there's only JS

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

Distribute terrain in zones 3 Answers

StartCoroutine in a public static void 1 Answer

GetComponent C# 1 Answer

Multiple Cars not working 1 Answer

Array index is out of range 2 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