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 elpulpobastardo · Oct 06, 2017 at 09:46 AM · instantiateprefabdestroyspawninstance

Instantiated object is destroyed in scene, but still logs as existing in console.

I am sure this is a simple beginner's error I am making, but I have tried everything I can think of and getting some help seems necessary now.

I have a ball which is spawned on Start (named 'Core'), and when that ball reaches the goal it is taken out of play and destroyed. This all works perfectly.

The Spawner is listening in update to see if the Core exists in the scene, and will Instantiate a new one if it is destroyed.

THE PROBLEM: The Core(clone) instance is destroyed in the scene, and it disappears as expected both in the game view and in the hierarchy of scene objects, but line 20-22 STILL logs out as the core existing in the scene. I just keep getting told it's still there somewhere, and so the new one won't spawn in it's place.

I have played with the SpawnCore() function to try and get it returning a GameObject, and trying to check for that, but it just tells me it's not accessible.

Hopefully it's an easy fix for someone, and if you could break down for me where I am going wrong it would help me learn a great deal and be very much appreciated!

Thanks!

 public class BallSpawner : MonoBehaviour {
 
     public GameObject corePrefab;
     public float respawnTimer = 1.0f;    //The delay in time in seconds until this ball respawns. Set to 0 for respawn the instant the ball is destroyed.
 
 
     // Use this for initialization
     void Start () {
         SpawnCore ();
     }
     
     // Update is called once per frame
     void Update () {
 
 
         if(corePrefab){
             Debug.Log ("CORE IN SCENE");
         }
 
         if(!corePrefab){
             Debug.Log ("THERE IS NO CORE IN THE SCENE");           <----------- Never gets called.
                 Invoke ("SpawnCore", respawnTimer);
         }
     }
 
         void SpawnCore(){
         GameObject coreInstance = Instantiate (corePrefab, transform.position, Quaternion.identity) as GameObject;
     }
 }

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 Dragate · Oct 06, 2017 at 10:09 AM

In corePrefab you have assigned the prefab, which is always there. In Update() you are checking for the prefab, not the instance (coreInstance), which you should.

Make coreInstance known to the entire class and check for that in Update().

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 elpulpobastardo · Oct 06, 2017 at 11:29 AM 0
Share

Thanks so much Dragate that worked. I hadn't fully understood the difference between the prefab and the instance.

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

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i need to destroy prefab clones 1 Answer

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

How do you destroy only one prefab gameObject? 1 Answer

Instantiate a Prefab with click in a certain area 2 Answers

Instantiated Prefab doesnt destroy 2 Answers


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