- Home /
Script is being applied to each prefab instead of one
Hi, sorry for the bad title, there is this script that allows objects to be dragged and dropped using the mouse, however it is dragging and dropping anything with a collider attached.
{
private bool mouseState;
private GameObject prefab;
public Vector3 Screen;
public Vector3 offset;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
prefab = GetClickedObject(out hitInfo);
if (prefab != null)
{
mouseState = true;
Screen = Camera.main.WorldToScreenPoint(prefab.transform.position);
offset = prefab.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Screen.z));
}
}
if (Input.GetMouseButtonUp(0))
{
mouseState = false;
}
if (mouseState)
{
var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Screen.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
prefab.transform.position = curPosition;
}
}
GameObject GetClickedObject(out RaycastHit hit)
{
GameObject prefab = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
prefab = hit.collider.gameObject;
}
return prefab;
}
}
I have tried making the "GameObject prefab" variable public and applying it the desired prefab, however, every object is still able to be dragged and dropped. How can I make this script apply to one gameobject.
Answer by Ady_M · Jan 01, 2019 at 04:32 PM
Your script is not checking if it's a draggable object.
if (prefab != null) is simply asking if you successfully clicked on any object that has a collider.
Currently, you have one instance of a script that raycasts to find out which object is clicked.
There's another solution: Try giving every draggable object a script that has the built-in methods, OnMouseDown and OnMouseUp (Raycast is not needed at all in this case). This script should have the mouseState, offset, etc. and the script should only be allowed to move the object it is attached.
If you insist on managing the drag & drop system the way you're doing it, it can of course be done. While explaining this, I will refer to your original script as the DragAndDropManager script.
You'll have to find a way to distinguish between draggable and non-draggable objects, for example by using tags or by attaching a Draggable script to the objects and do if (clickedObject.GetComponent () != null) inside the GetClickedObject method of the DragAndDropManager script. The Draggable script could simply contain an empty class and is used only to determine whether the object is draggable.
Finally, I would not recommend naming a variable "prefab" if it is also used to reference a specific object.
Ah, got it, I completely forgot about that function! Thank you!
Your answer
Follow this Question
Related Questions
Dragging an object in iOS (UnityScript) 3 Answers
Instantiate prefab from original position to click mouse point position 0 Answers
How to make Minecraft fences? 3 Answers
Using a raycast without colliders 0 Answers
LayerMask for RayCast 1 Answer