As an ambassador for LGBT-rights charity Stonewall, the screen legend known for movies as diverse as Lord of the Rings, X Men and Richard III visited Brighton College and opened his talk with a jokey warning that if pupils did not study hard for their exams then “You shall not pass!” – a film reference that caused the many Gandalf fans in the room to roar with approval.

Sir Ian told pupils from the Eastern Road independent school, along with visiting pupils from other Sussex schools, how he had come out as gay only when he was 49 during a BBC radio interview but wished he had done so sooner as his life was ‘immeasurably happy’ afterwards.

He said: “But when I was young, society was very different. The silence around homosexuality was deafening. There were no books in the library I could look things up in, no mention of it in school, there were no Clare Baldings or Graham Nortons to show it was normal. And there were no places to socialise if you were gay except members’ clubs where if you showed affection for someone of your own gender you could find yourself being arrested and thrown in prison if the police arrived.

“I don’t think I have ever recovered from that. It is a scar that comes from rejection and shame that you feel about yourself. The law is telling you that you are wrong. You are ‘queer’, second-rate, disgusting.”

He talked about how many gay people, feeling rejected by their families or by society, take to drink or drugs or even commit suicide “to look for some sort of escape.”

“I took up acting and doing that I could be whatever I wanted to be. I could express emotions That’s why a lot of professional actors are gay – they are trying to make something positive out of what used to be seen as something negative.”

A 2014 Stonewall report on homophobic bullying in schools revealed almost nine in ten secondary school teachers surveyed said pupils in their schools have experienced homophobic bullying while two thirds have heard homophobic language. Yet more than half of secondary school teachers say they don’t challenge homophobic language every time they hear it.

And fewer than one in five secondary schools teachers say their school stocks library books and information and LGBT issues.

Sir Ian praised Brighton College, which dropped its century-old uniform code in 2016 to accommodate gender dysphoric pupils, as a school that was working hard to tackle intolerance towards those who found themselves not in the majority, whether racial, sexual or religious, and urged pupils to create ‘a model society’ where difference was embraced and encouraged.

Headteacher Richard Cairns: “If children are happy and feel like they can be themselves, then they will achieve their potential. Sir Ian’s message was clear: Don’t treat anyone with less respect than you would like to be treated yourself. Accept that we are all different. Embrace that difference. And then discover how much more interesting everything is. Or as he might have put it: you shouldn’t live your entire life as an actor on someone else’s stage. You can be your own director, producer and performer.”