A guardian or next friend suing for an infant or person of unsound mind, and every plaintiff suing as an assignee, except an endorsee of a bill of exchange or a promissory note placed on the footing of a bill of exchange, when insolvent, may be required to give security for costs. On the failure to give security for costs within a reasonable time after it is directed by the court, upon the motion of the defendant, his or her action shall be dismissed.