Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sharckan · May 25, 2017 at 02:54 PM · 2dprefabinstantiate prefabdestroyimmediate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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";
             }
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sharckan · Jun 07, 2017 at 07:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges