Problem when it instanciates prefabs on Unity

41 Views Asked by At

I am not sure where I can post this thread because maybe the problem are about scripting, or maybe that is about modeling of tilemaps, or maybe that could be about the prefab configuration.

I have a tilemap generator in the right edge of each tilemap. That tilemap generate a prefab that is another tilemap with a lot of collected items lile coins and apples. sometimes the generator generates in a bad way the prefabs because some coins are disappeared or the child object of that collected item is appeared (it is a collected animation). Some of those bugs are only with the coin because I have prefabs of group of five, four, three or two coins (I guess that some problem can be on that).

Why sometimes does the instanciate make bad instanciates of my prefabs?

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


/**
 * Script para generar Tilemaps dependiendo del objeto padre.
 * Por ejemplo: Tutorial -> Comienzo, Comienzo -> Aleatorio y Aleatorio -> Aleatorio
 * 
 * 
 * 
 **/
public class GeneratorTimelap : MonoBehaviour
{

    [SerializeField] private GameObject[] tilemapAleatorios;
    [SerializeField] private GameObject[] tilemapTutorial;
    [SerializeField] private GameObject[] tilemapComienzo;

    [SerializeField] private Transform localizacionGenerar;
    [SerializeField] private GameObject gridObject;

    private Transform objetoPadre;

    private void Start()
    {
        objetoPadre = transform.parent;
        gridObject = GameObject.FindGameObjectWithTag("Grid");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Horizonte"))
        {
            if (objetoPadre.CompareTag("TilemapTutorial"))
            {
                int numAleatorio = Random.Range(0, tilemapComienzo.Length);
                GameObject tilemapGenerado = Instantiate(tilemapComienzo[numAleatorio], gridObject.transform);
                tilemapGenerado.transform.position = localizacionGenerar.transform.position;
            }

            if (objetoPadre.CompareTag("TilemapComienzo") || objetoPadre.CompareTag("TilemapAleatorio"))
            {
                int numAleatorio = Random.Range(0, tilemapAleatorios.Length);
                GameObject tilemapGenerado = Instantiate(tilemapAleatorios[numAleatorio], gridObject.transform);
                tilemapGenerado.transform.position = localizacionGenerar.transform.position;
            }
        }
    }
}

Other question that I want to say is that before I had a destructor in the left edge to destroy the instanciates that I had created but I've disconnected this gameobject and I have the same problem.

The player runs all the time to a right side position, the real player only jump, I don't think that the problem is about the velocity of the player but it is other thought that I've had.

2

There are 2 best solutions below

3
Shubham Jain On

Random.Range is max inclusive. Instead of using tilemapComienzo.Length as max, you should use tilemapComienzo.Length - 1

0
Pozo On

I've fixed the bug. I've used Resources.

The prefabs that you want to use an indeterminate number of times, they should move to a new directory inside assets directory called Resources.

You can instantiate those prefabs all times you want to with:

GameObject gameObject = Resources.LoadAll<GameObject>("OtherDirectoryInsideResourcesDirectory");

I post the API Unity link to Resources: https://docs.unity3d.com/ScriptReference/Resources.html