Let $X$ be a metric space. $f:X \rightarrow \mathbb{R}$ is upper semicontinuous if for all $\varepsilon>0$ and all $x \in X,$ there exists an open neighbourhood $U \ni x$ such that for any $y \in U,$ we have $f(x) + \varepsilon > f(y).$
For any real-valued function $f:X\rightarrow \mathbb{R}$, we define its upper semicontinuous envelope $Uf$ as follows:
For each $x \in X,$ $Uf(x)=\inf \{ \sup_{y \in V} f(y): V \text{ is a neighbourhood of } x\}.$
Prove that for any two bounded real-valued functions $f,g$ defined on $X$, $Uf = f$ if and only if $f$ is upper semicontinuous.
My attempt: Suppose $f$ is upper semicontinuous. Clearly we have $f \leq Uf$. Suppose $f < Uf.$ For each $x \in X,$ let $\varepsilon = Uf(x) - f(x) > 0.$ By the definition of upper semicontinuity, there exists an open neighbourhood $V$ of $x$ such that for all $y \in V,$ $f(x) + \varepsilon = Uf(x) > f(y)$. This implies that $Uf(x) \geq \sup_{y \in V} f(y)$, which further implies that $Uf(x) = \sup_{y \in V}f(y).$
I do now know what's wrong with my proof. I couldn't obtain a contradiction. Also, how to prove that if $Uf=f$, then $f$ is upper semicontinuous?
First show that $Uf$ is upper semicontinuous if it is a function $\to\Bbb R$ (i.e., $Uf(x)\ne\pm \infty$ for all $x$):
Let $x\in X$, $\epsilon>0$. Then by definition of $\inf$, there exists $V\ni x$ with $\sup_{y\in V}f(y)<Uf(x)+\epsilon$. But for all $z\in V$, this $V$ is also a neighbourhood of $z$, hence for such $z$ we have $Uf(z)\le \sup_{y\in V}f(y)$ as well and hence $Uf(z)\le Uf(x)+\epsilon$.
As a consequence, if $Uf=f$ then $f$ is upper semicontinuous.