Code that works in game give error in simulation