-1
\$\begingroup\$

I have two game objects with the same script.

enter image description here

In the PicUpSys script I have this code

using UnityEngine;
using System.Collections;

public class PicUpSys : MonoBehaviour
{

    public InvSys _InvSys;

    public bool SmallStone;
    public bool SmallTree;

    public GameObject ME;

    void Start()
    {
        ME = gameObject;
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            if (SmallStone == true)
            {
                if (_InvSys.SelectedSlot == "1")
                {
                    if (_InvSys.FirstSlotCont == "")
                    {
                        _InvSys.FirstSlotCont = "SmallStone (1)";
                        _InvSys.FirstSlotContN = 1;
                        _InvSys.Stone = _InvSys.Stone + 1;
                        Destroy(ME, 1);
                    }

                    else
                    {
                        _InvSys.FirstSlotCont = "SmallStone (" + _InvSys.FirstSlotContN + ")";
                        _InvSys.FirstSlotContN = 1;
                        _InvSys.Stone = _InvSys.Stone + 1;
                        Destroy(ME, 1);
                    }
                }

                else if (_InvSys.SelectedSlot == "2")
                {

                }

                else if (_InvSys.SelectedSlot == "3")
                {

                }
            }
        }
    }
}

When I start the game ME is automatically set to the current game object.

enter image description here

But when I click the SmallStone the script destroy SmallStone and SmallStone(1)

How I can do to only destroy SmallStone?

\$\endgroup\$
  • \$\begingroup\$ Why do you have a public GO that you'll set manually? Why not Destroy([this.]gameObject, 1) instead? It might sort out the issue as well. Otherwise you should also do something somewhere else causing this issue \$\endgroup\$ – user51515 Nov 3 '16 at 20:22
  • 1
    \$\begingroup\$ GetMouseButtonDown checks whether the mouse button is clicked. It does not check which object has been clicked. Consider implementing OnMouseDown if you want your object to react only when its attached collider is clicked. \$\endgroup\$ – DMGregory Nov 3 '16 at 21:10
  • \$\begingroup\$ @DMGregory its ok how it is i just want it to destroy the clicked object and not all \$\endgroup\$ – Pedro Nov 4 '16 at 13:46
-1
\$\begingroup\$

Try defining an object, in the code, as a variable. Then, go into the inspector, and set that game object to each stone as it's self. Finally, make the code destroy that game object variable.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Still not working i created the public Object Me; but still destroying everything \$\endgroup\$ – Pedro Nov 4 '16 at 13:45
  • \$\begingroup\$ Did you remove the line of code that sets varble "me" to the gameobejct? \$\endgroup\$ – The Mattbat999 Nov 4 '16 at 20:34
  • \$\begingroup\$ Yes i removed and created public Object Me; \$\endgroup\$ – Pedro Nov 4 '16 at 22:37
  • \$\begingroup\$ This is wrong, see DMGregory's comment on the question for the correct answer. \$\endgroup\$ – MichaelHouse Nov 28 '16 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.