Home > Blockchain >  If instantiate structure, i can't deteche collider in Unity3D
If instantiate structure, i can't deteche collider in Unity3D

Time:11-03

I make a random generator X structure and I won't make destroy objects if touch myself clone or X object, but the collider is not detecting. I checked the structure for generating to enable the collider or if the collider is a trigger, but all is okay. How I can fix it? Maybe because some objects are static in Inspector Unity?

My so-easy code:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckCollision : MonoBehaviour
{
    [Header("Косается?")] // touching?
    public bool TouchLog = false; // is default value

    private void OnCollisionEnter(Collision collison) // check to touch
    {
        if(collison.gameObject.name == "Leaves_LOD0") // ObjectName_LOD level
        {
            Destroy(gameObject); // destroy object what touch tree
            Debug.Log("Сталкновение!"); // touching! (test message)
        }
    }
}


CodePudding user response:

using static System.Random;
using UnityEngine;

public class TreeGenerator : MonoBehaviour
{
    [Header("Игровые объекты")]
    public GameObject Object;

    [Header("Лимит строений")]
    public int StuctLimit;

    [Header("Количество")]
    public int what;

    [Header("Максимум блоков для рендера")]
    public int maxRangeX;
    public int maxRangeZ;

    [Header("Рандомные координаты")]
    public int randomPosX;
    public int randomPosZ;

    private void Generating()
    {
        if(StuctLimit == 0)
        {
            System.Random randomPos = new System.Random();

            for (int i = 0; i <= what; i  )
            {
                randomPosX = randomPos.Next(1, maxRangeX);
                randomPosZ = randomPos.Next(1, maxRangeZ);

                Instantiate(Object, new Vector3(randomPosX, 0.65f, randomPosZ), Quaternion.identity);
            }
            StuctLimit  ;
        }
    }

    private void Start()
    {
        Generating();
    }
}

code for generating code

CodePudding user response:

Physics isn't updated until the next FixedUpdate call.

In order to manually force physics updates after each Instantiate of your object you could use Physics.Simulate

Physics.autoSimulation = false;

for (int i = 0; i <= what; i  )
{
    randomPosX = randomPos.Next(1, maxRangeX);
    randomPosZ = randomPos.Next(1, maxRangeZ);

    Instantiate(Object, new Vector3(randomPosX, 0.65f, randomPosZ), Quaternion.identity);

    Physics.Simulate(0f);
}

Physics.autoSimulation = true;
  • Related