Fear learning depends on prediction error, or the discrepancy between the actual and expected outcome of a conditioning trial. These experiments used blocking and unblocking designs to study the role of opioid receptors in the nucleus accumbens (Acb) in predictive fear learning. Previous fear conditioning to a context blocked later fear conditioning to a conditioned stimulus (CS) in that context. Fear learning proceeded normally (i.e., unblocking occurred) if the CS signaled a more intense footshock than was used during previous context conditioning. Blocking and unblocking were mediated by Acb opioid receptors. Acb microinjections of a nonselective opioid receptor agonist prevented blocking, whereas a nonselective antagonist prevented unblocking. Examination of the associative mechanism for blocking and unblocking revealed that Acb opioid receptors mediate indirect predictive learning by controlling learned variations in attention. Mu-opioid and kappa-opioid receptors contribute to this learned regulation of attention because Acb microinjections of a mu-opioid receptor agonist impaired, whereas a kappa-opioid receptor agonist facilitated, blocking. Acb microinjections of a mu-opioid receptor antagonist also prevented unblocking. Microinjections of a delta-opioid receptor agonist or antagonist were without effect on blocking and unblocking. Our data show that the Acb mediates attentional selection between competing predictors of motivationally significant events to enable learning about the best predictor of such events at the expense of worse predictors. During fear learning, Acb mu-opioid receptors upregulate attention to conditioned stimuli that are predictive of shock, whereas kappa-opioid receptors downregulate attention to conditioned stimuli that are redundant or noninformative predictors of shock.