$f\in\mathcal{O}_X(X)$ is a unit $\Leftrightarrow f_x\neq 0$ for all $x\in X$ Let $X$ be a scheme. I'm trying to prove the following:

$f\in\mathcal{O}_X(X)$ is a unit $\Leftrightarrow f_x\neq 0$ for all $x\in X$
[here $f_x$ is the germ of $f$ in the stalk $\mathcal{O}_{X,x}$]

EDIT: the correct statement should have "$f(x)\neq 0$" instead of "$f_x\neq 0$", where $f(x)$ is the equivalent class of $f_x\in\mathcal{O}_{X,x}$ in the quotient  $\mathcal{O}_{X,x}/\mathfrak{m}_{X,x}$ (residue field of the local ring $\mathcal{O}_{X,x}$). This is precisely the reason why I got stuck.
Based on other problems involving local objects, it's probably a good idea to consider the affine case first.
So let $X=\text{Spec}(A)$. I've already proven $(\Rightarrow)$, but I'm stuck at $(\Leftarrow)$.
The condition $f_x\neq 0$ for all $x$ is equivalent to $f\big|_{X_g}\neq 0\in\mathcal{O}_X(X_g)=A_g$ for all $g\in A$ with $X_g\neq \emptyset$. The condition $f\big|_{X_g}\neq 0$ amounts to $fg^n\neq 0$ for all $n\geq 1$. All this can be restated as $X_f\cap X_g\neq \emptyset$ for all $X_g\neq \emptyset$, i.e., $X_f$ is dense in $X$.
But in order to prove $f$ is a unit, I need $X_f=X$, which I can't see how to prove.
Any suggestions?
 A: Edit: To correct a previous error: I need to carefully explain what it means to evaluate a function on a locally ringed space. Let $(X,\mathcal{O}_X)$ denote a locally ringed space. If $f\in \mathcal{O}_X(U)$, and $p\in U$, we can define $f(p)$ as an element of the residue field $k(p)$ as follows. Send $f\mapsto f_p\in\mathcal{O}_{X,p}$ by the usual localization amp, and then send $f_p\to f(p)\in k(p)$ by sending $f_p\mapsto \overline{f_p}\in \mathcal{O}_{X,p}/\mathfrak{m}_p$, where $\mathfrak{m}_p$ is the unique maximal ideal of the local ring. To summarize, the map is $\mathcal{O}_X(U)\to k(p)$ by $f\mapsto f_p\mapsto \overline{f_p}$ and we call $\overline{f_p}=f(p)$. 
Example: If you don't believe that this is a sensible way to evaluate an element of a sheaf (thought of as a function on the space), you should try the following: take a polynomial function $f\in \mathcal{O}(\mathbb{A}^1_\mathbb{C})$ and apply this process at (say) the maximal ideal $\mathfrak{m}=(x)$ corresponding to $0$ in $\operatorname{spec}\mathbb{C}[x]$. We send $f\mapsto \frac{f}{1}$, and then we send $\frac{f}{1}$ to its residue class in $\mathbb{C}=\mathbb{C}[x]_{(x)}/\mathfrak{m}\mathbb{C}[x]_{(x)}$. Note that localization commutes with quotienting, so this is equivalent to quotienting first and then localizing, i.e. 
$$ \mathbb{C}[x]_{(x)}/\mathfrak{m}\mathbb{C}[x]_{(x)}\cong (\mathbb{C}[x]/\mathfrak{m})_{\overline{(x)}}\cong \mathbb{C}[x]/\mathfrak{m}.$$
The last isomorphism follows from the fact that the quotient of $\mathbb{C}[x]$ by a maximal ideal is already a field, so localizing further contributes nothing. So, we can actually evaluate the polynomial $f$ by sending $f\mapsto f\pmod{\mathfrak{m}}$. In this case, we are evaluating at the prime corresponding to $0$, which is $(x)$. So, we are sending $f\mapsto f\pmod{x}$. This is really just $f(0)$, after all. Indeed, let $f(x)=10x^3+5x+2$, then 
$$ f\equiv 2\pmod{x}$$
which is exactly $f(0)$.
Now, to the question. This depends on your definition of $X_f$. One definition of $X_f$ is $X_f=\{x\in X: f(x)\ne 0\}$, which  really means the set of $x$ at which $f$ does not vanish. In the affine case, another way is that $X_f=\{\mathfrak{p}: f\not\in \mathfrak{p}\}$. However, note that $f\not \in \frak{p}$ is equivalent to $f({\frak{p}})\ne 0$, so this is the same as the other definition above. Indeed:
$$ f(\mathfrak{p})=\overline{f_{\mathfrak{p}}}=0\iff f_{\mathfrak{p}}\in \mathfrak{p}A_{\mathfrak{p}}.$$
This in turn is equivalent to $\frac{f}{1}=\frac{p}{s}$ for $s\not \in \mathfrak{p}$ and $p\in \mathfrak{p}$. Now, this is equivalent to the existence of $t\not \in \mathfrak{p}$ such that $t(sf-p)=0$. This is equivalent to $tsf\in \mathfrak{p}$, which is equivalent (by $t,s\not \in \mathfrak{p})$ to $f\in \mathfrak{p}$. The moral is that indeed for the affine case
$$ X_f=\{\mathfrak{p}\in X:f(\mathfrak{p})\ne 0\}=\{\mathfrak{p}\in X: f\not\in\frak{p}\}.$$
By virtue of the fact that $f\not \in \frak{p}$ for all $\mathfrak{p}$, we see that $f$ is a unit. In particular, since $f$ is not contained in any maximal ideal $\frak{m}$ it must be a unit in the ring $A$. This shows the affine case, and you should try to use this idea to construct the general case. In particular, you should try to make sense of defining a section $\frac{1}{f}$ of $\mathscr{O}_X(X)$ given by $x\mapsto \frac{1}{f(x)}$ and explaining why it is a multiplicative inverse for $f$.
