N-narcosis, CO2 buildup...those can be the primary causes or simply contributing factors to your anxiety.
I had been diving for over 20 years before I felt my first bit of "high anxiety" on a dive. Neither of those factors were contributary.
Oddly enough, I was in less than 5 feet of water, collecting stone crabs for data collection as part of a research project. For some reason, my mask kept flooding. No big deal, usually. The vis was zero, but there were no other contributing factors I can think of right now (well, lots of drunk powerboaters were around, as I was diving on the edge of a channel under a bridge over the Memorial Day holiday---but I dont' think they were worrying me that much....).
Anyway, I suddenly felt overwhelmingly anxious. I couldn't breathe, I couldn't figure out what I was doing and I didn't want to be there anymore. I forced myself to slowly surface (not much more than "standing up"). No one knew what happened but it made me angry as much as anything. "What the hell happened? " was all I could wonder. I had hundreds and hundreds of dives and considered myself "panic proof" (hey, TOO much confidence is a bad thing).
I tried to "explore the feeling", to see if I could figure out what set me off and what I could do if it happened again. I deliberately removed my mask and spent most of the rest of the day without it or with it mostly flooded (couldn't see anyway). I continued to collect the crabs by feel and with my eyes exposed to that nasty water, but I couldn't replicate what happened. The feeling didn't return and I simply ended the day irritated with myself.
Then, a year or two later, the feeling came back on a dive at Stetson Bank. I made a free ascent in a current and found myself exhausted and breathing hard near the bottom. There was nothing to "hang to", so I couldn't just stop and "take a rest". It was very uncomfortable--I couldn't "catch" my breath and I found my movements were suddenly jerky and ill coordinated. All I could feel was the need "not to be here, right now" and a sense of helplessness.
I even worked on breath control, trying to slow down and deepen my breathing, but it didn't help. My inhalations were very stop-start ( I had to "force" deep breaths, because I wanted only to breathe shallow,but my inhalations were sorta staccato). My attempts to exhale deeply seemed equally ineffective. Gradually, I calmed down and I finished the dive normally and without incident by closing my eyes and thinking "pleasant' thoughts.
From that moment on, for the next two or three years, I would hope to make a "whole trip" without having one of the those "this ain't fun, what am I doing here?" dives. I wasn't so lucky. On trip, after trip, I would have 5 good dives and then, bang, one bad one.
I might have one great "yeehaw" dive in rough conditions, but during the next dive, even in dead calm, clear water, I might feel that old "Lord, this isn't fun" feeling return. I could fight it off, somtimes by closing my eyes, or holding onto a line for a moment or two, or sometimes just by "drifting" a bit in the current. I never had a full blown panic attack, but it sure wasn't fun.
For a year or so, I even felt uncomfortable on free dives and sometimes, just while swimming at the beach.
Finally, the feelings went away and I was able to dive freely and comfortably again, no matter what the conditions.
What changed? Some of it was related to stresses in "my dirt life" (land) that I learned to deal with effectively (job, sick child, that kind of stuff), some from a loss of diving skills. There was no epiphany, no sudden cathartic moment when I was "okay" and a good diver again, I simply went at it the old fashioned way: through practice, practice, practice. Gradually, over a period of a year, I regained my diving "composure".
Two years ago, I began swimming every day, even in the winter, if I could. I got where I could swim "laps" back on forth on a single breathold (My pool is moderately big, a little more than 45' in length)....
I am now to the point I can swim 4 of these laps U/W without much ado. I then started "towing" my 8 yr. old on my back on these laps, sans fins or mask for me. He simply taps me twice on my shoulder and I bring him up shallow where he releases . I then finish my swim on the same breath.
Why? That built confidence in my ability to free ascend at depth. For reinforcement, I also took to making more free dives on my trips, to the point I could easily go down quite deep and return without anxiety. Free diving was a skill I had mastered many years ago, but had lost.
Slowly, I started ENJOYING the dives again, instead of just enduring them (BTW, why didn't I just quit diving? Because I REMEMBERED how fun diving was, and I wanted that feeling back...)
For added scuba practice, I took to tossing my tank into the deep end of the pool (10') with only the regulator attached. The rest of the gear, I would spread randomly around the pool. Then I jump in and gear up without surfacing, saving the mask for last.
On other pool sessions, I would impulsively flood my mask, or dismount and remount my gear without surfacing.
On dives, I took to "not worrying" about my SAC so much. I used to have the best SAC of any diver in any group I went on. But, after those few "anxiety" experiences my SAC went up, way up. That made it worse, because, like you, I worried about running low on air too soon and ruining things for my buddy or the group. The more I worried, the more friggin' air I breathed.
After I stopped worrying about SAC, darn if it didn't go up and up, nearly to where it was when I was 18.
I am working hard to get back in shape, after getting really fat (a big part of my problem). When my son first got sick, I gained 70lbs, mostly by eating "his" hospital food for most of the two years he was in treatment. I was a sedentary and bloated 270lb fat guy.
These days, I work out four of five times a week in the gym and I ride my bike on the weekends. I still weigh 240lbs, but I'm losing weight slowly and comfortably and I'm in very good shape.
On any given week, I ride my bike 20 miles three times , and take one 45 mile single ride on the weekends. All this, including some century rides, I do all this, btw, on a mountain bike with knobbies (Hey, still too fat for my Peugeot).
I lift weights and work cardio every day (rotating groups). I am as strong at 45 as I was when I played HS football and I feel as well as I did then, too.
Next month I riding in a 100K bike ride as part of my qualification for a 4400 mile ride next year. I still need to lose some serious weight, but I will get there, no problem.
As for diving? I still display caution and try not to ovedue it on dives. I use the granny line whenever possible and I try hard not to get winded--even a little bit. In a bad current, if at all possible, I go hand over hand.
If I have to free swim, and if I do get tired and find myself breathing heavily, I just "let go" and breathe as deeply and freely as I feel the need. I don't try to "hold back", because this can cause CO2 buildup, triggering panic as your cues for breathing are fooled into thinking you don't have enough air when you do.
Mostly though, through practice, I overcame a loss of skills and confidence that had accrued over many years of intermittent diving. Through excercise I gained the reserved I needed to handle tough conditions.
As for you? Assuming you are in good shape, then IMHO, your experience level was likely not where it should have been for the level of responsibility you were assigned. You might have been nearly there, but close isn't good enough. A few more deeper dives, in a current, even setting lines, but without students to worry about might have been all you needed to get you to where you could handle "one more task" without overloading. You were trying to do too much and "worrying" about too much.
Practice some more, particularly some more in dark, dirty water or in a currents, similar to the conditions where you felt uncomfortable the first times, and you might be surprised at how quickly you regain your confidence with a little "exposure".
Don't worry. Few admit it, but most divers have felt anxiety on a dive or dives (more than 50% according to DAN, report having suffered similar feelings on at least one dive in their experience).
Ricky...I ain't got no anxiety no mo'.......