So far the gameObject is created, but I need to click on the object a second time, in order to start the Drag event. Here's the script where I use the button to spawn a prefab
public void SpawnPrefab(Transform prefab)
{
clicked = false;
Vector3 mousePosition, targetPosition;
mousePosition = Input.mousePosition;
targetPosition=Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,distance));
prefab.position=targetPosition;
if(isAlreadyClicked == false)
{
Instantiate(prefab, prefab.transform.position, prefab.transform.rotation);
isAlreadyClicked = true;
Debug.Log(clicked);
}
}
I call this Drag event from another script inside the spawned gameObject though
void OnMouseDrag()
{
cursor.setGrab();
if(this.transform.parent == null || this.transform.parent != null)
{
//drag the component
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
if(this.gameObject.CompareTag("Component"))
{
mainCanvas.transform.GetChild(1).gameObject.transform.GetChild(0).gameObject.transform.GetChild(4).gameObject.SetActive(false);
}
if(this.gameObject.CompareTag("NewComponent"))
{
mainCanvas.transform.GetChild(1).gameObject.transform.GetChild(0).gameObject.transform.GetChild(4).gameObject.SetActive(true);
}
}
}
Hope it's clear enough. Any ideas? Thanks in advance!
EDIT: So I found out that the SpawnPrefab() only works when mouse button is released. Is there any way I can make the buttons work on mouse pressed?