What makes people behave kindly? Is it the result of having been nurtured in an environment of love and kindness that makes you more likely to treat others the same way? Or perhaps personal experiences of threat and deprivation make you more attuned to the suffering of others? Or maybe it’s just a matter of genes?

As with so many human tendencies, displays of kindness are likely to be influenced by both environment and genes. People who have genes that predispose them to empathy and kindness, for example, are steadfast in their charitable behavior, regardless of their current environment, a new study finds. But people with genes that are linked to a weaker inclination toward altruism tend to reduce their charitable behavior and commitment to civic responsibilities, such as political action or jury duty, when they have heightened feelings of fear or being threatened.

The study included 348 American residents with European ancestry. Researchers gauged participants’ overall sense of the world as a socially safe or threatening place, by noting how much they agreed with statements like “Human nature is basically good” or “There is more good in the world than bad.”

Researchers also asked about the participants’ charitable activities and commitment to civic duties. Charitable behaviors included volunteering, giving blood and participating in community groups. Civic commitment was measured by examining participants’ responses to questionnaire items related to serving on a jury or paying taxes, even when those duties seem onerous or seemed unfair.

Participants’ DNA was also examined for the presence of three genes that previous research has linked with social behavior and altruism. The first was a gene that influences the brain receptor for oxytocin, a hormone linked with trust and with the forging of intimate human relationships, between family members, lovers or friends.

The gene comes in three variants: A/A, A/G or G/G. The G/G version has previously been linked with greater maternal sensitivity to children, increased empathy, more positive emotion and greater ability to cope with stress. It also carries a reduced risk for social anxiety and autism. The A/A version has the opposite characteristics, with the A/G variant falling in between.

Two genes for another receptor, this one for the hormone vasopressin, which is also involved in social connections, were also studied. The genes AVPR1a RS1 and AVPRa RS3 similarly have three versions each (in this case short/short, short/long and long/long) and the long version of both genes is also linked with greater empathy, less social anxiety and lower autism risk. In male rodents, vasopressin receptor levels can predict which animals will be monogamous and which promiscuous, and other variations in the human AVPR gene have been linked to lowered marital quality and relationship problems in men.

The new research found that people who reported the world as being a more scary and threatening place were less likely to engage in charitable activities, but only if they had the A/A or A/G oxytocin receptor gene variants. Similarly, if people felt that the world was unsafe and that others couldn’t be trusted, they showed less civic commitment, but again, only if they had one or two short versions of the vasopressin RS1 gene. RS3 was not linked with any differences.

The study couldn’t determine whether it was early childhood experiences or current experiences (the surveys were mainly done in late 2002, which means that the 9/11 attacks were probably still on people’s minds) or other factors that contributed to the participants’ sense of threat. However, the findings fit well with other research suggesting that the effects of positive social experiences and stress counteract: stress and threat tend to reduce altruism, while altruism and social connection alleviate stress. As the authors write: “It is widely accepted that having a strong social network and high quality close relationships promotes physical and mental well-being.”

What the new study reinforces, however, is that those who are genetically predisposed to be kind may be less socially deterred by threat, but those for whom socializing itself can produce anxiety may tend to become even more withdrawn than to reach out in times of fear.

And that’s frightening in a country where the overall sense of threat and lack of trust has hit historic highs — as evidenced by people’s responses to the question “Generally speaking, would you say that most people can be trusted or that you can’t be too careful in dealing with people?” Since 1970, the General Social Survey has been polling thousands of people on that question and in 2010, nearly two-thirds said you “can’t be too careful,” compared with just half in 1970.

The good news, however, is that at least in the population surveyed for the current study, the majority (51.5%) had the highly altruistic G/G oxytocin receptor gene that wasn’t affected by threat, while only 7.2% had the risk-bearing A/A variant. Moreover, these genes only account for a small amount of variation in behavior and many studies that find these types of connections later fail to be replicated.

Nonetheless, understanding that threat can make us less likely to connect — even when connection is the best way to reduce threat — may help everyone make healthier choices.