x


Easily cleaning up \ deleting unused variables?

During the course of completing a project I generally end up with several variables throughout my scripts that are declared but end up not being needed.

Is there anything out there that can run through scripts, find which variables are declared but not used, and then give a list of what and where they are?

I'm about to write my own little .exe or editor script (maybe even an UltraEdit or TextPad macro) that does that but before I spend the time I wasn't sure if something like that already existed. Anyone know of one?

more ▼

asked May 08 '10 at 02:04 AM

Ony gravatar image

Ony
888 29 36 48

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If you use C#, then there are plenty of refactoring tools available. I would strongly recommend ReSharper. It will help you do hundreds of things to improve your code, including deleting unused variables.

Its even smart enough to detect variables that are assigned to, but never read from and other esoteric cases that you may not think of if you roll your own script.

more ▼

answered May 08 '10 at 02:21 AM

Michael La Voie gravatar image

Michael La Voie
2.3k 7 12 43

I'm using Javascript on this project so ReSharper won't work on this, but thanks for the heads-up. I'll check it out when I work more with C#.

May 08 '10 at 02:42 AM Ony
(comments are locked)
10|3000 characters needed characters left

You shouldn't need any external tools to do this. Unity should show you any unused variables in the console window as an warning (with a yellow warning icon). In particular it warns you about:

  • variables which are never used at all
  • variables which are assigned, but their value is never used
  • unreachable code
  • cases where your code compiles, but is probably wrong (i.e. where a comparison will always evaluate to false)

If you double click a warning message in the console, it will open the script editor and take you to the relevant line.

Unused variables and unreachable code warnings in the Unity IDE

Edit:
Actually it seems as though these errors are only generated for C# scripts in unity, not Javascript scripts.

more ▼

answered May 10 '10 at 03:27 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Is there something I need to set somewhere to have it do this? As of right now it shows neither variables which are never used at all, nor variables which are assigned but their value is never used. It shows code errors, etc. of course, but nothing in there with warnings about unused variables.

May 10 '10 at 06:50 PM Ony

Not that I'm aware of - for me, unity does this out of the box. Which version of Unity are you using? (regular/pro? iphone?)

May 16 '10 at 01:57 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5061
x3324
x819
x341
x11

asked: May 08 '10 at 02:04 AM

Seen: 2038 times

Last Updated: May 08 '10 at 02:04 AM