Destroy GameObject when its clicked on

2020-02-15 06:41发布

I'm making a building mechanic in my game and I want to be able to clear out certain objects around the map (trees, other decor) so I have room to build. I've tried using a ray cast to find what object is being clicked on and destroy it but that doesn't work.

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

public class ObjectDestroy : MonoBehaviour {
    // Start is called before the first frame update
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            Debug.Log (Input.mousePosition);
            if (Physics.Raycast (ray, out hit)) {
                if (hit.collider.gameObject == gameObject) Destroy (gameObject);

            }
        }

    }
}

2条回答
祖国的老花朵
2楼-- · 2020-02-15 06:59

You don't need to add this script on every object, just add it to a manager and also I think you are missing Raycast parameters.

To see where you ray is going you can use Debug.Ray()

Also, I would prefer you use @MSauer way since is much cleaner for what you want, just be sure the object contains a collider, I think they can be a trigger and the click will still happen.

查看更多
放我归山
3楼-- · 2020-02-15 07:04

Here is a little example script:

public class Destroyable : MonoBehaviour
{
    private void OnMouseDown()
    {
        Destroy(gameObject);
    }
}

You can attach this script to the GameObject you want to destroy and then during Play-Mode you can click on it to destroy it. It is modifiable if you just need it in your In-Game-Editor.

Note: You need an active Collider on the same Gameobject.

Edit:

The following script shows an example for changing the color of the object:

public class Destroyable : MonoBehaviour
{
    public Color mouseHoverColor = Color.green;
    private Color previousColor;
    private MeshRenderer meshRenderer;

    private void Start()
    {
        meshRenderer = GetComponent<MeshRenderer>();
        previousColor = meshRenderer.material.color;
    }

    private void OnMouseDown()
    {
        Destroy(gameObject);
    }

    private void OnMouseOver()
    {
        meshRenderer.material.color = mouseHoverColor;
    }

    private void OnMouseExit()
    {
        meshRenderer.material.color = previousColor;
    }
}
查看更多
登录 后发表回答