My gameObject disappear after using mouse's position

Hello everyone,

I was coding a gameObject can follow the mouse position but the gameObject disappear after starting the test. I have tried to change or add someting to my code but nothing change.

The code :

Testing :

please help me to find the problem. (My opinion is with the Z position but I don’t know what I can do)

Thank You.

Honestly i should try the unity’s UI. Set up a canvas and put an image with the sword sprite or image and let it follow your mouse. I am not sure but it should follow input.MousePosition like that. I do not know why is vanishes. Are you making the gameobjects while in playmode or do you have other scripts that might alter the gameobject?

Sorry I have forgot to put my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseFollow : MonoBehaviour
    public GameObject Sword;
    public Vector2 mousePosition;

    void Update()
        mousePosition = Input.mousePosition;
        Sword.transform.position = new Vector2(mousePosition.x, mousePosition.y);
        Debug.Log("Mouse's position : " + mousePosition);
        Debug.Log("Object's position :" + Sword.transform.position);