.enabled = false not working
I am extremely new to Unity and coding in general and I am making a game with a grapple hook in it. The grapple hook activates but does not disappear upon releasing the button. I have tried using GetKey Instead GetButton but that doesnt work ( Not even to activate the grapple). I think it might be a problem with the bottom two lines but I am not sure
using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using UnityEngine;
public class GrapplingHook : MonoBehaviour { DistanceJoint2D joint; Vector3 targetPos; RaycastHit2D hit; public float distance = 5f; public LayerMask mask; public LineRenderer line; public float step = 0.02f;
// Start is called before the first frame update
void Start()
{
joint = GetComponent<DistanceJoint2D>();
joint.enabled = false;
line.enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire2"))
{
targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = 0;
hit = Physics2D.Raycast(transform.position, targetPos - transform.position, distance, mask);
if (hit.collider != null && hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
{
joint.enabled = true;
joint.connectedBody = hit.collider.gameObject.GetComponent<Rigidbody2D>();
joint.connectedAnchor = hit.point - new Vector2(hit.collider.transform.position.x, hit.collider.transform.position.y);
joint.distance = Vector2.Distance(transform.position, hit.point);
line.enabled = true;
line.SetPosition(0, transform.position);
line.SetPosition(1, hit.point);
}
if (Input.GetButton("Fire2"))
{
line.SetPosition(0, transform.position);
}
if (Input.GetButtonUp("Fire2"))
{
line.enabled = false;
joint.enabled = false;
}
}
}
}
Answer by Bunny83 · Jul 15, 2020 at 08:23 PM
You have your GetButton and GetButtonUp check inside your GetButtonDown if body. So they won't be called since you can not have a GetButtonDown and a GetButtonUp at the same time ^^. So check your brackets and indention.
Your answer
Follow this Question
Related Questions
Need Raycast2d to point where my ship is looking 0 Answers
General 2D Top Down Help 0 Answers
When I build and run my project it seems that its lacking, plz help?! 0 Answers
How to speed up building at game? 1 Answer
Post time at the end of the level 0 Answers