0
\$\begingroup\$

I'm currently working on a game strategy game and I just started working on it.

Here is the first script:

using UnityEngine;

public class Village : MonoBehaviour
{
    public int strength;
    private int x;

    private void Start()
    {
        StrengthValue();
        strength = x;

        print(gameObject.name + " ---- " + strength);
    }

    private void Update()
    {
        if (Input.GetMouseButton(0) && GameObject.Find("Game Manager").GetComponent<GameManager>().ArmySize > strength)
        {
            Destroy(gameObject);
        }
    }

    private void StrengthValue()
    {
       x = Random.Range(10, 500);
    }
}

here is the second script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public int ArmySize = 100;
}

All I want is for the game to detect when the player clicked a certain village. But I just can't figure it out since I'm pretty new to c# and unity. The scripts should be self-explanitory, but if you need more details please ask.

\$\endgroup\$
5
  • \$\begingroup\$ Did you mean to use OnMouseDown, or to cast a ray and destroy the object that the ray hit, instead of every object that's independently listening to mouse clicks destroying itself? \$\endgroup\$
    – DMGregory
    Aug 28 '21 at 14:01
  • \$\begingroup\$ That was a tiny mistake. I meant to use GetMouseButton. Also, How could I implement your suggestion onto my script(s)? \$\endgroup\$
    – DavidA_
    Aug 28 '21 at 14:03
  • \$\begingroup\$ I'd recommend searching "unity onmousedown" or "unity raycast" — the examples in the documentation should cover what you need, and this is also covered extensively in beginner Unity tutorials. Even searching past Q&A here should turn up examples you can use. \$\endgroup\$
    – DMGregory
    Aug 28 '21 at 14:06
  • \$\begingroup\$ thank you, I didn't know 'OnMouseDown' was a thing. I decided to use that instead 🙏 \$\endgroup\$
    – DavidA_
    Aug 28 '21 at 14:11
  • \$\begingroup\$ If you've solved your problem, please post your solution as an Answer below. \$\endgroup\$
    – DMGregory
    Aug 28 '21 at 14:17
1
\$\begingroup\$

Instead of putting this:

if (Input.GetMouseButton(0) && GameObject.Find("Game Manager").GetComponent<GameManager>().ArmySize > strength)
{
    Destroy(gameObject);
}

in the Update function, I put it into the OnMouseDown function. I also made sure to put colliders on my objects.

so this is the full script:

using UnityEngine;

public class Village : MonoBehaviour
{
    public int strength;
    private int x;

    private void Start()
    {
        StrengthValue();
        strength = x;

        print(gameObject.name + " ---- " + strength);
    }

    private void OnMouseDown()
    {
        if (Input.GetMouseButton(0) && GameObject.Find("Game Manager").GetComponent<GameManager>().ArmySize > strength)
        {
            Destroy(gameObject);
        }
    }

    private void StrengthValue()
    {
       x = Random.Range(10, 500);
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .