Is your builder “code component” at the top of your routine? Your routine runs in the order you place them in the builder menu. Basically, you may be trying to place your stimulus before it is defined in the code component. I know this mistake all too well…
To quote the “Code Components” page directly… " Be aware that the code for each of the components in your [Routine] are executed in the order they appear on the [Routine] display (from top to bottom). If you want your Code Component to alter a variable to be used by another component immediately, then it needs to be above that component in the view. You may want the code not to take effect until next frame however, in which case put it at the bottom of the [Routine]. You can move Components up and down the [Routine] by right-clicking on their icons." from: https://www.psychopy.org/builder/components/code.html