Destroying a cube when another cube is at a certain position?

Ok, after getting my cube to move to another cubes position i have ran into another problem. This problem is that when the cube reaches its destination, the other cube should be deleted, but it isn’t!!:rage::

CODE:

if (GameObject.Find("movePosBlock(Clone)") == null)
            {
                Vector3 g = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
                if (Input.GetMouseButtonDown(1))
                {
                    
                    Instantiate(movePosBlock, g, Quaternion.identity);
                    iTween.MoveTo(stuartCharacter, g, 5);
                    
                }
                if (stuartCharacter.transform.position.Equals(g)) {
                    Destroy(GameObject.Find("movePosBlock(Clone)"));
                    Debug.Log("Deleted Waypoint!");
                }
            }

You’re only going into the if at the top if it can’t find it (==null). So of course you cant find it to destroy it later in that code block.

I move it so the if statement was out side the other if statements:

 void Update()
    {

        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        Vector3 g = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));

        if (Physics.Raycast(ray, out hit))
        {
            if (GameObject.Find("movePosBlock(Clone)") == null)
            {
                
                if (Input.GetMouseButtonDown(1))
                {
                    
                    Instantiate(movePosBlock, g, Quaternion.identity);
                    iTween.MoveTo(stuartCharacter, g, 5);
                    
                    //stuartCharacter.transform.position = g;
                    
                }
            }
        } 
        if (stuartCharacter.transform.position == g) {
                    Destroy(GameObject.Find("movePosBlock(Clone)"));
                    Debug.Log("Deleted Waypoint!");
                }
        
    }

Still didn’t work

Of course it doesn’t. Floating point numbers don’t compare well, especially if you round one of them before comparison. You should be checking the distance between the position and g, not that they are the same. (and don’t round them). If 1 unit = 1 meter, and 1 centimeter is close enough, you would get the vector between the two positions, and check that the length is < 0.01.

edit: (vector1 - vector2).magnitude will give you the distance.

Still won’t work no matter what i do :frowning:

void Update()
    {



        Ray ray = camera.ScreenPointToRay(Input.mousePosition);



        Vector3 g = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));



        if (Input.GetMouseButtonDown(1))
        {
            if (GameObject.Find("movePosBlock(Clone)") == null)
            {

                if (Physics.Raycast(ray, out hit))
                {
                    Vector3 gLocation = g;
                    Instantiate(movePosBlock, g, Quaternion.identity);

                    iTween.MoveTo(stuartCharacter, g, 5);
                     //stuartCharacter.transform.position = g;
                   }

            }
            

        }
        if (((stuartCharacter.transform.position - g).magnitude < 5))
        {

            Destroy(GameObject.Find("movePosBlock(Clone)"));

            Debug.Log("Deleted Waypoint!");
        }
    }
if (GameObject.Find("movePosBlock(Clone)") == null)

Here you are saying :

if the GameObject with name "movePosBlock(Clone)" doesn't exist (is null)
{
      // do this...
}

if it exists, nothing is going to happen. You can check if it does exist by doing this:

if (GameObject.Find("movePosBlock(Clone)") != null)

this will execute the code below if the object exists (isn’t null)

then in there you are doing a raycast, why?

if your first block of code works (in the first post), the only thing you need to change
is the second if statement:

if (stuartCharacter.transform.position.Equals(g))

this should use this

float dist = Vector3.Distance(other.position, transform.position);

if (dist <= 0.01)
{
    //do the destroy here

}

of course you should actually define your variables outside the update function and avoid doing searches (GameObject.Find) per frame as well.

Thanks got it working :slight_smile: