2
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ What is your question ? I didn't get it. \$\endgroup\$ – Shashimee Jul 6 '17 at 7:32
  • \$\begingroup\$ I have multiple buttons where I can spawn different prefabs on button click, and the spawned prefab will be dragged when the mouse button is pressed, by means when I click the button, I want both dragging and spawning can be executed at the same time, and the prefab will be destroyed if player released the mouse. Until now I still can't figure out how to do it. \$\endgroup\$ – Mira Jul 6 '17 at 7:43
  • \$\begingroup\$ What i think is ; you are making a drag and drop system? Clicking on UI button, instantiating a game object on mouse position ? If i am correct i could assist you more \$\endgroup\$ – Thalthanas Jul 6 '17 at 14:54
  • \$\begingroup\$ @EmreE yes you are correct \$\endgroup\$ – Mira Jul 7 '17 at 1:26
1
\$\begingroup\$

When you spawn the element, set the isDragging to true.


using UnityEngine;
using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour , IPointerUpHandler, IBeginDragHandler {

    public bool isDragging;

    void Update () {
        if (!isDragging)
            return;
        // Put you dragging logic here
    }

    public void OnBeginDrag(PointerEventData data){
        isDragging = true;
    }

    public void OnPointerUp (PointerEventData data){
        isDragging = false;
    }


}
\$\endgroup\$
  • \$\begingroup\$ I put this code on the UI button that spawns the prefab, but this seems to be only working after I released the mouse button \$\endgroup\$ – Mira Jul 7 '17 at 2:11
  • \$\begingroup\$ or am I missing something? T.T \$\endgroup\$ – Mira Jul 7 '17 at 2:21
  • \$\begingroup\$ Thsi should be on you spawnable object. And right after u spawn it, set isDragging to true \$\endgroup\$ – Guilherme Junqueira Coutinho Jul 7 '17 at 3:08
  • \$\begingroup\$ The reason I'm using a UI buttons is I will be able to spawn different prefabs in the scene based on which button I choose. Thinking about using an Update() to spawn the prefab and using you code as well, but I'm not sure if I could use the same Update() for every button with different prefabs yes? \$\endgroup\$ – Mira Jul 7 '17 at 3:38
  • \$\begingroup\$ Thsi Update goes inside the Spawned object, you have to write the instantiation elsewhere, in anoyher script, and link it to the button. \$\endgroup\$ – Guilherme Junqueira Coutinho Jul 7 '17 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.