- Home /
How to avoid instant GetMouseButtonDown-checking when instantiating object with button?
I am instantiating objects when I press ui-buttons. I want to move them around until as I click again, but unfortunalty because of pressing the ui-button it releases it instantly. Even if I set a bool flag to check on the next Frame it dont work.
void Update () {
if(currentObj)
MoveObject();
}
void MoveObject(){
//Movement
currentObj.position = Input.mousePosition;
if(instantiated){ //Check on next frame to avoid instant placing
if(Input.GetMouseButtonDown(0))
currentObj = null;//Release it
} else
instantiated = true;
}
public void InstantiateObj(string obj){
instantiated = false;
currentObj = Instantiate(Resources.Load(obj), Vector3.zero, Quaternion.identity) as Transform;
}
Answer by unidad2pete · Sep 03, 2017 at 10:08 PM
I hope this is you want:
public bool IHaveObject;
void Update()
{
if (currentObj)
{
MoveObject();
}
if(Input.GetMouseButtonDown(0))
{
if(IHaveObject)
{
IHaveObject = false;
currentObj = null;
} else
{
IHaveObject = true;
InstantiateObj("YOUR OBJECT");
}
}
}
void MoveObject()
{
currentObj.position = Input.mousePosition;
}
public void InstantiateObj(string obj)
{
currentObj = Instantiate(Resources.Load(obj), Vector3.zero, Quaternion.identity) as Transform;
}
Thanks but the objects are instantiated from ui-buttons with specified string parameter thats why the instantiate method is public and has a string parameter.
No problem, move IHaveObject = true inside of your InstantiateObj function and remove the ELSE of " if(IHaveObject) "
Your answer
Follow this Question
Related Questions
How to make my new Instances spawn in the right direction? 1 Answer
Checking if object intersects? 1 Answer
(Unity 2D C#) Move instantiated prefab on Y axis? 1 Answer
How can I move an object to a clicked objects (center of X,Z) coordinates (board game)? 0 Answers
"Working with moving and mouse touch to set coordinates" 0 Answers