I have this character that jumps on left click:
public class Punch : MonoBehaviour
{
public Rigidbody2D rb;
public bool clicked;
public Vector2 direction;
public static Punch instance;
// Start is called before the first frame update
void Start()
{
instance = this;
rb = GetComponent<Rigidbody2D>();
clicked = false;
direction = new Vector2(500f, 250f);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && (clicked == false))
{
rb.AddForce(direction);
print("left click");
clicked = true;
}
}
In order for the camera to follow this character, I made the camera a child of the character. This works fine. Now I added an object “Button” in the top left corner of the camera and made it a child of the camera to follow the camera exactly (and stay in the top right).
When I use this script on my button:
private void OnMouseDown()
{
Punch.instance.clicked = false;
print("TEST");
}
it does not print “Test” or reset the “clicked” variable to false.
If the button is not a child of a camera, but on the same hierarchy as the character, the button works but obviously won’t follow the camera / character.
What am I doing wrong and how can I fix this?