In theory, legislators have a sworn duty to only pass laws that are constitutional. See United States Constitution, Article VI, Section 3:
The Senators and Representatives before mentioned, and the Members of
the several State Legislatures, and all executive and judicial
Officers, both of the United States and of the several States, shall
be bound by Oath or Affirmation, to support this Constitution; but no
religious Test shall ever be required as a Qualification to any Office
or public Trust under the United States.
But, in practice, the only remedy someone has available if the legislature passes an unconstitutional law is to have a court determine that the law is invalid, which a court will only do in the event of an actual live dispute over its validity (the case or controversy requirement see U.S. Constitution, Article III, Section 2, Clause 1) by someone actually materially affected by the law (the standing requirement).
Legislators have absolute immunity from lawsuits over passing unconstitutional laws. See U.S. Constitution, Article I, Section 6 (in the pertinent part):
They shall in all cases, except treason, felony and breach of the
peace, be privileged from arrest during their attendance at the
session of their respective Houses, and in going to and returning from
the same; and for any speech or debate in either House, they shall not
be questioned in any other place.
Similarly, a legislature is usually considered to be an organ of the government rather than an entity in its own right that can be sued for money damages or injunctive relief, in most cases. Instead, it has governmental immunity from lawsuits.
Indeed, the fact that courts can overturn an unconstitutional law is one reason that legislators feel less obligated to make sure that laws that they pass are constitutional than they would under a regime of parliamentary sovereignty in which there was no form of judicial review of legislation available, because they don't have the last word on the question. In a regime of parliamentary sovereignty, legislators have a greater personal moral responsibility to uphold democratic and human rights norms in their legislation. See, e.g., here.
The idea behind the case and controversy and standing rules is that the American legal system interprets laws in a very context and fact dependent way, and also that the American legal system has always had comparative few judges relative to other legal systems. Indeed, often, it is impossible to know if a law is unconstitutional or not under the relevant legal standard until it is applied. So, this rule limits lawsuits that may or may not affect anyone (to conserve judicial resources), and requires that there be a context to illuminate and illustrate what the law means in a real world context rather than in the abstract.
The standing requirement also prevents people who actually like an unconstitutional law from bringing a sham lawsuit to declare it invalid and then intentionally losing the lawsuit. See, e.g., Antonin Scalia, "The Doctrine of Standing As An Essential Element Of The Separation Of Powers", 17 Suffolk University Law Review 881, 891 (1983).
As noted above, these requirements are rooted in Section 2, Clause 1 of Article III of the United States Constitution and the legal doctrines developed around it which have been interpreted to prohibit the filing of advisory opinions.
A minority of U.S. states and many countries have a different process that allows minorities in the legislature and certain other officials to seek advisory opinions on the constitutionality of laws passed by a legislature before it takes effect.
Sometimes these questions are ruled on by a "Supreme Court" (see, e.g., the advisory and suo moto powers of the Supreme Court of India and the power of the Supreme Court of Canada to issue "reference opinions"), sometimes by a body called a "Constitutional Court" (see, e.g., Germany's Federal Constitutional Court) and sometimes by a body called a "Council of State".
The Founders of the United States in the Constitutional Convention that produced the current U.S. Constitution which took effect in 1789, considered creating a similar body in the United States called a "Council of Revision" under a plan proposed by the Virginia delegation. But, this proposal was expressly rejected because judicial review of the constitutionality of legislation by the federal courts would provide sufficient means to ensure the constitutionality of legislation, and the institution would therefore be redundant.
More generally, pre-enactment constitutional review of legislation is not a core part of the U.S. legal culture, which it derived largely from the legal culture of England at the time of the American Revolution. This common law legal tradition had no historical traditional of judicial review of legislation at that time. Even today, after many reforms of its judicial system, the newly created U.K. Supreme Court can only hear appeals of lower court cases, much like the U.S. Supreme Court outside its very narrow trial court jurisdiction, and does not have the authority to issue advisory opinions.