Search Unity

Resolved Instantiate clones array destroyer

Discussion in 'Prefabs' started by xOSSxKratos, Nov 14, 2022.

  1. xOSSxKratos

    xOSSxKratos

    Joined:
    Apr 4, 2021
    Posts:
    11
    Success! I'm so happy I'll share the experience to help out others. I looked all over for the answer to this problem, then had a eureka moment when I figured out the solution.


    Summary:

    The Problem:
    Original prefab gets destroyed with its clones on destroy due to tag name.
    Spawner broke because it had no prefab to clone.

    The Solution:
    Created a clone Tag called "Clone".
    I gave the clones a new tag(Clone) as they were instantiated with cube.tag = "Clone";
    Changed the FindGameObjectsWitheTag to ("Clone").


    The long story with details:

    The problem was:
    I had a spawner. It spawned clones of a prefab(Taged: MiniGamePrefabs) on mass. When the player leaves the area or game over, the score goes to 0, the collection of prefabs GameObject[].FindGameObjectsWithTag("MiniGamePrefabs") gets destroyed. Problem is when the player goes back into the area the spawner has no prefab to reference, the original prefab gets destroyed because it's tag("MiniGamePrefabs"). I saw a few complicated solutions, but you always want to take the simplest approach if you can.



    --Below is the ResetScore script with the FindGameObjects[] to create the list of tagged objects.

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

    public class ResetScore : MonoBehaviour
    {
    private int resetScore = 0;

    private void OnTriggerEnter(Collider collider)
    {
    if(collider.gameObject.CompareTag("Paws"))
    {

    ****//Creating the list of objects to be destroyed here//****
    GameObject[] argo = GameObject.FindGameObjectsWithTag("MiniGamePrefabs");
    foreach (GameObject go in argo)
    {
    ****//This is when the list gets destroyed//****
    Destroy(go);
    }
    ****//Resets all minigames scores to 0//****
    //reset score of all mini games of saber type
    ScoringSystemCatSaber.catSaborTheScore = resetScore;
    SquilSaber1ScoreManager.squilSaber1TheScore = resetScore;
    SquilSaber2ScoreManager.squilSaber2TheScore = resetScore;
    SquilSaber3ScoreManager.squilSaber3TheScore = resetScore;
    StinkSaberScoreManager.stinkSaberTheScore = resetScore;
    ScoringSystemToySaber.toySaberTheScore = resetScore;
    CarTrouble1ScoringSystem.carTroubleTheScore = resetScore;
    CarTrouble2ScoringSystem.carTrouble2TheScore = resetScore;
    CarTrouble3ScoringSystem.carTrouble3TheScore = resetScore;

    }
    }
    }



    --Below is the Spawner script that spawns GameObject cube on mass.

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

    public class Spawner : MonoBehaviour
    {

    public GameObject[] cubes;
    public Transform[] points;
    public float beat = (60/130)*2;
    public float timer;

    void Update()
    {
    if (timer>beat)
    {
    GameObject cube = Instantiate(cubes[Random.Range(0, 4)], points[Random.Range(0, 4)]);
    ******* cube.tag = "Clone"; ******* //This is the one line of code added to fix my problem//
    cube.transform.localPosition = Vector3.zero;
    cube.transform.Rotate(transform.forward, 90 * Random.Range(0, 4));
    timer -= beat;
    }
    timer += Time.deltaTime;
    }
    }



    The Solution:
    Created a clone Tag called "Clone".
    I gave the clones a new tag(Clone) as they were instantiated with cube.tag = "Clone";
    Changed the FindGameObjectsWitheTag to ("Clone").