G
G
Gleb Coller2022-02-11 17:01:51
C++ / C#
Gleb Coller, 2022-02-11 17:01:51

Does not delete the desired object, how to fix it?

I wrote a code that creates objects (in my case, logs), moves them, and when it comes into contact with objects with the "deleter" tag, it is deleted. At the very least, they should be removed. I can not understand what is wrong, why the object is not deleted.
Here is the code itself.

using UnityEngine;

public class oakSpawner : MonoBehaviour
{
    public GameObject oak;
    private GameObject OakClone;
    private float[] distance = new float[] { -0.916f, 0.916f };
    private float moveSpeed = 0.01f;
    public void Start()
    {
        OakCreator();
    }
    public void OakCreator()
    {
        int position = UnityEngine.Random.Range(-1, distance.Length);
        Vector3 randpos = new Vector3(position, -0.12f, -20f);
        OakClone = Instantiate(oak, randpos, Quaternion.identity);
    }
    public void Update()
    {
        OakClone.transform.Translate(0, 0, moveSpeed);

    }
    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "deleter")
        {
            Destroy(gameObject);
        }
        else
        {
            Debug.Log("нихуя сабе");
        }

    }
}

Help someone is not difficult, you have a minute, and I have new knowledge in c #.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2022-02-11
@FishSti4k

Collider OnCollisionEnter triggering conditions:
- presence of the Collider component (BoxCollider, SphereCollider)
- both objects
have Trigger disabled - at least one object has a Rigidbody
- object layers are not disabled for collision in the physics settings
You can also set Debug.Log(other.gameObject.tag) to make sure that OnCollisionEnter() gets objects with the right tag. Visually, the contact of the gizmo colliders should also be visible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question