How can I spawn an object with a mouse click down, and destroy it when I release the button?
I have the angle of tragectory and physics down, but it won't be destroyed when I release the button. Here's my code:
void Throw(){
//Throw axe if fire1 is pressed
if (Input.GetButtonDown ("Fire1")) {
GameObject axe = Instantiate (pickaxeObject, playerTrans);
pickaxerb = axe.GetComponent<Rigidbody2D> ();
PointToMouse (axe);
//Find mouse position
Vector3 mousePos = Input.mousePosition;
//change z axis of mouse position
mousePos.z = 10f;
//Convert to world space
mousePos = Camera.main.ScreenToWorldPoint (mousePos);
//Find player position
Vector3 playerPos = axe.transform.position;
//Calculate difference between target and player
Vector3 diff = mousePos - playerPos;
//Throw Axe
diff = diff.normalized; //Normalize diff
pickaxerb.AddForce (diff * thrust, ForceMode2D.Impulse);
/* Doesnt work
if(Input.GetButtonUp("Fire1")){
Destroy(axe);
}
*/
}
}
I think instantiating the disabled object and enabling/disabling when I need it to would work, but I'm not sure how to do that. I know that getting the components not in start isn't performant, but I'm not sure how else to do it. Thanks for any help!
Input.GetButtonUp
won't work inside Input.GetButtonDown
private GameObject axe;
private void Update()
{
if (Input.GetButtonDown ("Fire1"))
{
axe = Instantiate (pickaxeObject, playerTrans);
pickaxerb = axe.GetComponent<Rigidbody2D> ();
PointTo$$anonymous$$ouse (axe);
//Find mouse position
Vector3 mousePos = Input.mousePosition;
//change z axis of mouse position
mousePos.z = 10f;
//Convert to world space
mousePos = Camera.main.ScreenToWorldPoint (mousePos);
//Find player position
Vector3 playerPos = axe.transform.position;
//Calculate difference between target and player
Vector3 diff = mousePos - playerPos;
//Throw Axe
diff = diff.normalized; //Normalize diff
pickaxerb.AddForce (diff * thrust, Force$$anonymous$$ode2D.Impulse);
}
if(Input.GetButtonUp("Fire1") && axe != null)
{
Destroy(axe);
}
}
When I try that, it says The name "axe" does not exist in the current context.
I've figured it out! I put the GetButtonUp if statement outside of GetButtonDown and used GameObject.FindGameObjectWithTag to access it.
Even better on the system, you could just make the object raycast track the mouse, and enable/disable the meshrenderer when clicking.
Answer by DTek · Feb 22, 2018 at 11:40 PM
@RomulanNinja - you can setup a boolean that tells you when the object is there.
bool objectInstantiated = false;
void Update() {
if(Input.GetButtonDown ("Fire1")) {
if(!objectInstantiated) {
objectInstantiated = true;
InstantiateObject(); // Instantiate your object in this function;
}
} else {
if(objectInstantiated) {
objectInstantiated = false;
RemoveObject(); // destroy your object in this function
} } }
Alternatively, you can check if the reference for the object is null : if ( myObject != null) { ... }
Your answer
Follow this Question
Related Questions
How to create buttons with script? 0 Answers
No overload for method. 1 Answer
How do I limit an object to rotate back and forth between 2 angles? 1 Answer
Side-scrolling Enemy AI not shooting in the right direction 0 Answers
How to make a random object generator that responds to simple touch? 0 Answers