koki1019
koki1019

Reputation: 23

Unity not destroying object on one click

So im making this building tycoon kinda game and i experienced this issue

    public GameObject particle;

    public GameObject[] building;

    private void OnMouseDown()
    {
        if(GameManager.Manager.buildingID >= 0)
        {
            Instantiate(building[GameManager.Manager.buildingID], transform.position, 
            Quaternion.identity);
            Instantiate(particle, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }

When i click on it it instantiates the prefabs but does not destroy the object that i clicked on But if i click it on again then it destroys it

I even tried: DestroyImmediate(gameObject); Destroy(this.gameObject); and DestroyImmediate(this.gameObject); (like these make any difference but still tried it)

I decided to go to destroying it cause when i used bools to check if it was build on that space, itll just reset the bool to False after like 3 - 5 seconds

I even tried creating a new C# script and pasting the code in

I have no idea whats going on, even without instantiating the objects it still does not delete it in one click

Upvotes: 0

Views: 177

Answers (1)

dem611
dem611

Reputation: 1

Where is this script attached? When you use

Destroy(gameObject);

gameObject refers to the GameObject that the script is attached to, not necessarily the object that you are clicking on, so this script should be destroying the object that it is attached to.

Upvotes: 0

Related Questions