- Home /
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.
Hi! Can you please edit your post to add the code as text ins$$anonymous$$d of image? This will help us quote it, read it, and help you debug it. Thanks!
Hello. Bad post ... First, Paste the code as a text, not as an image, so we cane asy copy and edit it to give you some answer.
Second. what disapear means? What gameobject? The sword? Specyfy it.
third. I see the sword is still in the scene, just have so hugue position values...
Answer by Zentiu · Feb 28, 2020 at 04:54 PM
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?
Answer by donutgamesindie · Feb 28, 2020 at 02:50 PM
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);
}
}
Your answer
Follow this Question
Related Questions
Reverse object position order 1 Answer
2D C# destroy a GameObject on collision 2 Answers
Itween gradual jump 2d 1 Answer
One GameObject, two scenes 2 Answers