Mathematical modeling of gravitating configurations of physical fields is one of the priority directions of the modern theory of gravity. Most of the exact solutions constructed within the framework of the general relativity are static or stationary configurations. This is due to the objective complexity of solving the Einstein equations under the assumption of nonstationarity. We present an approach to constructing nonstationary configurations of a spherically symmetric nonlinear real scalar field and the electromagnetic field, which are assumed both to be minimally coupled to gravity. It is based on the separation of one invariant equation, written in terms of the characteristic function and scalar field potential. Using the proposed method, an exact nonstationary solution with a nontrivial topology of space-time will be constructed.