Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision

Discussion in 'Prefabs' started by yepp73, Jan 29, 2021.

  1. yepp73

    yepp73

    Joined:
    Jan 2, 2021
    Posts:
    5
    Hi.
    I'm new to unity, but made a object(Man) walking around.
    Then I spread some balls(Bluberry) using this code.

    Code (CSharp):
    1. for (int y=0; y<numBlueberry; ++y)
    2.        {
    3.             GameObject inst = Instantiate(Blueberry, new Vector3(Random.Range(-20.0f, 20.0f), 1, Random.Range(-20.0f, 20.0f)), Quaternion.identity);
    4.             inst.name = "Blueberry1";
    5.        }
    Then I try to detect collision.
    Code (CSharp):
    1. private void OnCollisionEnter(Collision collision)
    2.     {
    3.             LogCollsiionEnter.text = "On Collision Enter:" + collision.gameObject.name;
    4.             //Destroy(collision.gameObject);
    5.         }
    6.     }
    The Bluberry1 name is not appearing.
    If the man walk into an other object witch is not a prefab , the name will show.

    If I do the destroy it will work. But I'm not able to sort out by name or tag.

    Also if I don't tick 'is trigger' the man stops when walking into a blueberry.

    I have also tried collision.collider.name but same result.

    What is it I'm getting when it comes to Prefabs and collision ?

    I need to be able to sort on collision.

    Thankful for all help.

    Greeting Kaizen.
     
    Last edited: Jan 29, 2021
  2. yepp73

    yepp73

    Joined:
    Jan 2, 2021
    Posts:
    5
    I found that the problem is the actual name of the prefab. Depending on some the situation (Can not figure out what is causing what) the created gameobject from prefab got a different name. Sometimes the name + (clone).
    Strange behavior...