- Home /
Error destroying prefab
public class Dices : MonoBehaviour {
public GameObject Dice4_1;
public GameObject Dice4_2;
public GameObject Dice4_3;
public GameObject Dice4_4;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
int result = Random.Range(1, 5);
if (GameObject.Find("Dice4_1_img(Clone)") != null)
{
Destroy(Dice4_1);
}
if (GameObject.Find("Dice4_2_img(Clone)") != null)
{
Destroy(Dice4_2);
}
if (GameObject.Find("Dice4_3_img(Clone)") != null)
{
Destroy(Dice4_3);
}
if (GameObject.Find("Dice4_4_img(Clone)") != null)
{
Destroy(Dice4_4);
}
if (result == 1)
{
Instantiate(Dice4_1, transform.position, transform.rotation);
}
if (result == 2)
{
Instantiate(Dice4_2, transform.position, transform.rotation);
}
if (result == 3)
{
Instantiate(Dice4_3, transform.position, transform.rotation);
}
if (result == 4)
{
Instantiate(Dice4_4, transform.position, transform.rotation);
}
}
}
}
This gives me an error that says: To avoid lose of data bla bla bla, you should use DestroyInmediate(Object, true);
Then I change it, and it says, that DestroyInmediate doesnt exist in this contexts.
What I want is that each time I click on the object, it creates a prefab of another object (1 between 4), and if any of those 4 exists, then it should delete it, and then create the prefab that the RandomRange says.
Answer by ShadyProductions · May 24, 2017 at 08:49 AM
Add a tag to the dices called: "dice" and then try something like this:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Dices : MonoBehaviour
{
private List<GameObject> DicePool;
/// <summary>
/// Lazy loaded dice pool
/// </summary>
public List<GameObject> DiceObjects
{
get
{
//if the pool is not empty, return our pool
if (DicePool.Any()) return DicePool;
//if pool is empty, then fill the pool and return the pool
DicePool = GameObject.FindGameObjectsWithTag("dices").ToList();
return DicePool;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
int result = Random.Range(1, 5);
if (DiceObjects.Any())
{
//Destroy each dice
foreach (var dice in DiceObjects)
{
Destroy(dice);
DicePool.Remove(dice); //remove from the pool
}
}
if (result == 1)
{
var go = (GameObject)Instantiate(Dice4_1, transform.position, transform.rotation);
go.tag = "dice";
}
if (result == 2)
{
var go = (GameObject)Instantiate(Dice4_2, transform.position, transform.rotation);
go.tag = "dice";
}
if (result == 3)
{
var go = (GameObject)Instantiate(Dice4_3, transform.position, transform.rotation);
go.tag = "dice";
}
if (result == 4)
{
var go = (GameObject)Instantiate(Dice4_4, transform.position, transform.rotation);
go.tag = "dice";
}
}
}
}
Ok, I tried the pools, but it was confusing, so I didn't use pool, and still I found a solution.
public class Dices : $$anonymous$$onoBehaviour {
public GameObject Dice4_1;
public GameObject Dice4_2;
public GameObject Dice4_3;
public GameObject Dice4_4;
// Use this for initialization
void Start () {
Dice4_1.SetActive(false);
Dice4_2.SetActive(false);
Dice4_3.SetActive(false);
Dice4_4.SetActive(false);
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
int result = Random.Range(1, 5);
if (result == 1)
{
Dice4_1.SetActive(true);
}
else
{
Dice4_1.SetActive(false);
}
if (result == 2)
{
Dice4_2.SetActive(true);
}
else
{
Dice4_2.SetActive(false);
}
if (result == 3)
{
Dice4_3.SetActive(true);
}
else
{
Dice4_3.SetActive(false);
}
if (result == 4)
{
Dice4_4.SetActive(true);
}
else
{
Dice4_4.SetActive(false);
}
}
}
I did that and the objects called there are images, not prefabs, or 2D sprites. It works fine.
PS: $$anonymous$$aybe I just used a pool and I don't even know it lol.
Your answer
Follow this Question
Related Questions
Instantiated Prefab doesn't appear? 3 Answers
Reusing tilemap segments 0 Answers
[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 0 Answers
Trouble with tilemaps as prefabs for 2D plattformer 1 Answer
How to check for the nearest gameObject in OverLapCircleAll? 1 Answer