As you can see, there are any number of ways to come up with a melody. You just need to find a way that works for you. Have a go at any or all of the above suggestions - I've tried them all and sometimes they work, sometimes they don't. Like most things in the creative sphere, it can be very hit and miss.
What works well for me, more often than not, is to gather together some lyrical ideas revolving around a concept that forms the nucleus of the song to come - the spark of inspiration. Then I jam away on the guitar, looking for ways to develop that kernel musically. Sometimes it takes hours, sometimes it doesn't come at all. But the best melodies seem to just materialise somehow and generally within minutes.
When that doesn't happen, it's often best to find something else to do - or go back to the lyrical ideas and work from that angle.
It sounds like I'm an advocate for writing the words first, but I'm not. That has never really worked for me.
What I'm saying is that without any kind of thematic concept to work with, whatever melody you do come up with is going to be pretty formless and haphazard until it finds something tangible to lock onto.
So you do need something to write about, even if its ephemeral, before you start looking for a tune.
That's how it's always worked for me anyway.
cheers,
L