Let $X$ be a variety and $Z\subset X$ be a closed subvariety. Denote by $Z^c$ the set-theoretic complement in $X$ together with the structure as subvariety of $X$. Assume that there are morphisms $f: Z\to Y$, $f^c: Z^c\to Y$ where $Y$ is another variety. Then define $$F: X\to Y, x\mapsto\begin{cases}f(x),& x\in Z,\\ f^c(x),& x\in Z^c.\end{cases}$$
I am searching for a proof or reference of the following: if $F$ is continuous then it is a morphism. This should hold true but I was unable to find it in my textbooks.
Two brief remarks:
1) If we drop the assumption that $f$ should be continuous, the assertion is false for sure.
2) If we would know that $X$ is the coproduct of $Z$ and $Z^c$, the assertion would follow. But I don't expect this to be true in general.
This seems to be related to morphism of ringed spaces glue but I'm a little confused as in this question, continuity always seems to hold. But it there is also assumed that $Z$ and $Z^c$ should be open so it can't be directly applied to my question.
References and hints to textbooks/articles are very welcome.