This paper deals with the existence and uniqueness of weak solution for a class of obstacle problem of the form \(\begin{aligned} {\left\{ \begin{array}{ll} &{}\displaystyle \int _{\Omega }\mathcal {V}(x,Dw):D(\vartheta -w)\mathrm {~d}x+\displaystyle \int _{\Omega }\left\langle w\vert w\vert ^{p(x)-2},\vartheta - w\right\rangle \mathrm {~d}x\\ &{} \quad \ge \displaystyle \int _{\Omega }\mathcal {U}(x,w)(\vartheta -w)\mathrm {~d}x, \\ \;\\ &{} \vartheta \in \Im _{\Lambda , h}, \end{array}\right. } \end{aligned}\) where \(\Im _{\Lambda , h}\) is a convex set defined below. By using the Young measure theory and Kinderlehrer and Stampacchia Theorem, we prove the existence and uniqueness result of the considered problem in the framework of generalized Sobolev space.