instantiate within OnTriggerEnter, c# question.

Hi everyone,

Slowly trying to learn through the many examples and active Unity Community - getting a little stuck on something, though:

I want a player to hit an object and activate an instantiation that creates many more objects.

I did something to the extent of the following, but it is not working:

void OnTriggerEnter(Collider other)
{

    if (other.tag == "Player")
    {

        for (int i = 0; i < 200; i++)
        {
            Instantiate(prefab, new Vector3
                        (Random.Range(-50, 50), -324,
                         Random.Range(-50, 50)), Quaternion.identity);

            prefab.transform.localScale = Vector3.one * Random.Range(-8f, 8f);

        }

Any ideas?

Make sure you pass the prefab in the inspector and also make sure that your OnTriggerEnter method working fine.

Make sure rigidbody added to the object which you are colliding. Check out this page: Unity Manual - Colliders Scroll to the bottom and look at the two tables. They show you what combinations of collider, trigger, rigidbody, isKinematic will collide together. Next, make sure the objects are not on collision layers that are set to ignore each other.

Also read this documentation https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html