It's hard to tell what's going wrong from a screenshot. The best way to post this would be a simplified version of your project in a zip file, but the new forum software doesn't allow that, so you'd have to post a link from somewhere else.
If I were you, I'd use the debugger to step through the code and make sure that you're actually getting a working object at that point, that the object does have a texture, and that the texture object still exists. When you find the point at which it ceases to exist, you can probably figure out what's going wrong.
By the way, the only difference moving your script would make is the paths you'd have to use to find something, assuming the object the script is attached to continues to exist. It should function the same anywhere.