When doing that for exercises/exams/etc, I want also to have a separate numbering for equations and floats (figures and tables). So my solution is to have this:
% requires ifthenelse package
\newboolean{Solucion} % True/False to show the solution
\setboolean{Solucion}{true} %
% define the color we use to typeset the solution, requires xcolor
\colorlet{darkgreen}{green!40!black}
\colorlet{darkblue}{blue!40!black}
\colorlet{solcolor}{darkblue}
\usepackage{environ}
\newcounter{soleq}
\makeatletter
\NewEnviron{solution}{\ifSolucion\color{solcolor}%
\let\c@equation\c@soleq\def\theequation{S\thesoleq}%
\expandafter\BODY\fi}
\makeatother
\usepackage{newfloat}
\DeclareFloatingEnvironment[name={\textcolor{solcolor}{Figura}}]{solfigure}
\renewcommand{\thesolfigure}{\textcolor{solcolor}{S\arabic{solfigure}}}
% This is for use with babel[Spanish,English], adapt to your case
\addto\captionsenglish{\floatname{solcolor}{Figure}}
\addto\captionsspanish{\floatname{solcolor}{Figura}}
This outputs the solution in a different color, and the equations are numbered independently from the main part --- so that the equation and float numbers do not change between the document with and without the solution.

You probably need to tweak that a bit (I use Spanish and English with babel
, for example), and it could be made better (notice the color of the figure caption), but I think it's a good starting point.