Firstly: Nobody really knows for sure the answer to this question. It’s still an open area of research. This answer is just a summary of the best rough guess we have right now.
Your brain does important fixes and maintenance work when you sleep. Why it can’t do this while you’re awake, we don’t know, but given the evolutionary advantage any species could get by *not* needing to sleep, it’s likely that there’s a good reason why you *have* to sleep.
As the cleanup and fixes that haven’t been done pile up, the brain begins to have problems. Neurotransmitters don’t get replenished as they should. Connections fire that shouldn’t fire, and other connections don’t fire that should. Chemical imbalances of other kinds can also occur. More or less, the brain just starts shutting down and going haywire, and one of the ways it can go haywire is by making you experience sensory inputs that aren’t real.
One speculative possibility: You mentioned the whole memory-transfer thing makes sense to you. Well, what if the brain were *trying* to do that transfer while you were still awake? Wouldn’t that be putting things that aren’t real–sensations you *previously* had, but aren’t having right now–into the same parts of the brain that do the thinking stuff? If so, then maybe the hallucinations are because of that.
Again, *nobody* has hard scientific evidence for the specific reason, so every answer you get will be at least partially speculation.
Latest Answers