OnMouseDown()

I have a problem with the OnMouseDown() function
unfortunately it doesn’t work for me and I have no idea, could someone please send me the code to make it work for me instead of this one

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Waypoint : MonoBehaviour

{

[SerializeField] GameObject towerPrefab;

[SerializeField] bool isPlaceable;

void OnMouseDown()

{

    if(isPlaceable)

    {

        Instantiate(towerPrefab,transform.position,Quaternion.identity);

    }

}

}

Hi,

In the Lecture Project Changes, which can be found in the Resources of most videos, you can find the instructor’s code.

And if OnMouseDown() still does not work, please check the description in the API.

I hope this helped. :slight_smile:


See also:

iam using new imput systema and code with OnMouseDown() dont work ¨
iam using now this code but when i click it place tower on all placeable block

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.InputSystem;

public class Waypoint : MonoBehaviour

{

[SerializeField] GameObject towerPrefab;

[SerializeField] bool isPlaceable;

private void Update()

{

    // Získáme vstup z nového Input Systemu

    Mouse mouse = Mouse.current;

    if (mouse != null)

    {

        // Zkontrolujeme, zda bylo stisknuto levé tlačítko myši

        if (mouse.leftButton.wasPressedThisFrame)

        {

            OnClick();

        }

    }

}

 void OnClick()

{

    if (isPlaceable)

    {

        // Vytvoříme věž použitím Instantiate

        Instantiate(towerPrefab, transform.position, Quaternion.identity);

    }

}

}

Good job! :slight_smile:

Yes, the new input system might need a different solution. At least, people in this thread stated that OnMouseDown does not work with the new input system anymore.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms