How to fix Index Out Of Range errors in Unity3D

A very common problem in Unity programming is the Index Out Of Bounds Exception

A related cousin is the Argument Out Of Range Exception.

Both mean the same thing. One is for arrays, one is for other types of collections.

What it means is you have a collection of items, but you’re asking for something outside of that collection.

For instance if you have three things, you are asking for the fourth (or greater) one.

Keep in mind that if you have three things, they will be indexed by 0, 1, 2.

If you ask for thing at index 3, that is the fourth thing and it is out of range of a three-thing collection.

Another tricky part of this one is that often when you doubleclick on the error, Unity does not take you to the offending line of code.

However, you can still get there manually by looking at the error text itself.

Select the error in the console window and then look down in the lower panel of the console window. Find the first line that is your script, and that will contain a number that tells you where the error is. In some newer versions of Unity you can even click in that window to be taken there.

If after all that you are baffled by why you are getting this error, and you are POSITIVE that there are enough items in your collection to satisfy your dereference operation, then you need to put a Debug.Log() call right before the point where you get the error, and print out two things: the index you are dereferencing, and the Count (or Length) of the collection itself.

Now you have some information to go track down why in fact you do not have enough items in the array.

How to report a problem in the Unity3D forums

Many people struggle with properly reporting a problem on the Unity3D forums. This post is to try and help you get it right and speed up your return to success.

Step 1. Choose a good subject line. Don’t choose “Help!” or “Doesn’t work” Instead say “Need help with inventory system” or “Getting an X error when I pick up an item.”

Step 2. Do a little bit of basic research on your own. Double-click the actual error and try to see what code it relates to. Google the error text itself. Read at least two or three results. You will NEVER be the first person to have this error, trust me.

Step 3. Identify at least the ballpark area where the error is in your code. Extract the relevant section of code, as little as you think will show the problem, and post it here. If you don’t post code, we can’t help you. If you post massive amounts of code, we’ll assume you’re not even trying to helpful.

Step 4. When you post code, use CODE tags (in square brackets) to format your code so we can read it.

Step 5. Tell us the following things and be very precise:

1. What you think it should be doing

2. What it actually is doing

3. What things you have tried in order to fix it

4. Any other interesting tidbits you think might be relevant

Keep in mind the point of the Unity forum is not for us to write your script for you. It’s for YOU to write your script, and when you get into trouble, come ask questions in the forums, paying attention to the notes above.

Also if you are following a tutorial, none of us here are going to go and try out the tutorial to see where you have the problem. It falls on you to first understand it well enough to present your issue in terms of the above requirements, otherwise we simply are not going to be able to help you.

How to find a nullref in Unity3D scripting

I am developing a standard “How to handle a nullref” post, since it is the most-common and (usually) simplest form of error encountered when scripting in Unity3D.

Steps to find a Null Reference (nullref) error:

In the console, doubleclick the error to figure out where it is in your code.

What is in that line of code that could be null? (reference types obviously)

Is it a complicated hairy line of code? Then break it down into constituent statements, run it again, find where the error is now

When you know which variable is null, now ask yourself:

  • what mechanism is supposed to set that variable up? (UnityEditor? GetComponent? GameObject.Find()? Something else?)
  • why is it not being set up? (unable to find, misconfigured, something else?)
  • fix it.

You can do this. Trust me.

ALSO: often one nullref begets 999+ more… start with the very first one at the very top of your log, because often it will trigger all the others.

Tank Dynamaze has arrived for Android TV!

You can play Tank Dynamaze on your Android TV! Get it here:

https://play.google.com/store/apps/details?id=com.plbm.dynamaze2dtv

Use either a single DPAD controller or a dual-analog setup to drive your tank through an ever-changing dark maze, looking for the elusive master enemy bunkers to destroy.

Mix it up with enemy helicopters, enemy tanks and enemy bunkers and blast your way to the best global score!

Demo Night Tonight (Meetup)

I’ll be demoing tonight at the Orange County Unity3D Meetup. Details here:

Demo night/lightning rounds and holiday gathering (Unity & IGDA)

Tuesday, Dec 18, 2018, 6:30 PM

Mobilityware
440 Exchange #100 Irvine, CA

61 Developers Attending

Happy Holidays! This next event we’re going to have a walk-around networking & demo night and some lightning rounds. Lightning rounds are five minute quick demos. We’re joining our two groups (OC Unity and IGDA OC) together for this meetup. Bring whatever you want to demo that you are working on. Be it a game, VR Experience, tool, art work, or som…

Check out this Meetup →

You can get all my games here:

Hovercraft Project including full project source

I made a little hovering project since this is a common Unity3D question.

It has both a simple cube that hovers, but also features a driveable hovercraft.

It all uses the Unity3D physics system. Click image to play it now!

http://plbm.com/hovering/index.html

And here is the full project including all source code:

https://www.bitbucket.org/kurtdekker

or

https://www.github.com/kurtdekker

Leave a comment if you have any questions!

Easy Unity3D UI connections – DATASACKS!

I wrote a little package called “Datasacks” that are basically Unity ScriptableObjects and can be plugged into various UnityEngine.UI components to ease the process of integrating with your game code.

The basic idea is that you no longer need to drag references from your scenes/prefabs into your code. Instead you drag references to these Datasacks, and then the UI components or your code interoperates directly with those Datasacks.

For efficiency these components set up a small network of bidirectional listener/observer patterns in order to make it all very slim on Unity runtime resources.

All the source is available here at:

https://www.github.com/kurtdekker

https://www.bitbucket.org/kurtdekker

There’s a little mini example game inside of it showing off a lot of the components, as outlined in the above diagram.