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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by TheDeceit · Nov 18, 2013 at 04:29 AM · null reference

C# Array Index Out of Range

Hello.

I come from an AS3 background and the C# way of doing things is throwing me for a loop. I'm trying to create an array of transforms for a tile editor. Whenever I try to check if the transform is null I get an out of range exception. I'm guessing that I haven't initialized it, but it looks good when I compare it with the code on MSDN.

Here's the code in question. Any assistance would be greatly appreciated!

Editor.CS

 void UpdateTile ()
 {
     if (tile == null || !inRange)
         return;
 
     if (deleting)
     {
         if (map.instances[index] != null)
         {
             DestroyImmediate (map.instances[index].gameObject);
             map.instances[index] = null;
         }
     }
     else
     {
         if (map.instances[index] == null)
         { 
             var instance = Instantiate (tile) as Transform;
             instance.parent = ship.transform;
             instance.position = new Vector3 (cursorX * map.gSize, cursorY * map.gSize);
             map.instances[index] = instance;
         }
     }
 }

TileMap.CS

 public class TileMap: MonoBehaviour 
 {
     public float    gSize        = 1.0f;
     public int    gColumns    = 6;
     public int    gRows        = 6;
 
     public Transform[] instances = new Transform[1000];
 }
Comment
Add comment · Show 1
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 supernat · Nov 18, 2013 at 04:52 AM 0
Share

Can you post the code related to "index"? Your code looks fine relative to index, if index is < 1000. What's the exact error?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Nov 18, 2013 at 05:22 AM

Put this:

public Transform[] instances = new Transform[1000];

into a Start routine. Can't trust out-of-function initialization.

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 TheDeceit · Nov 19, 2013 at 04:34 AM 0
Share

That did it! There are a myriad of other issues with my code, but the out-of-range error is now taken care of. Thank you.

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

19 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 avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException: Object reference not set to an instance of an object script_bullet.OnTriggerEnter (UnityEngine.Collider other) (at Assets/script_bullet.js:35) 1 Answer

NullReferenceException Error 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

How to import the object from server to unity 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