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 /
This question was closed Jun 08, 2020 at 10:12 PM by hosterweis for the following reason:

Other

avatar image
0
Question by hosterweis · May 16, 2020 at 02:00 PM · gameobjectprefabienumeratorinstantiate prefabstartcoroutine

Object Not Instantiating After Being Destroyed

Hello, I have a GameObject in my game called Wind and I am trying to spawn the wind on the right side of my screen every 3 seconds. The wind then moves across the bounds of the screen, then is destroyed once it is far enough off of the screen. The first wind object, which is always off the screen to begin with, does this properly, but once it is destroyed, no new wind prefabs are instantiated. The code for the wind is here:

 public class WindSpawn : MonoBehaviour
 {
     public float speed = 5f;
     private Rigidbody2D rb;
     private Vector2 screenBounds;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = this.GetComponent<Rigidbody2D>();
         rb.velocity = new Vector2(-speed, 0);
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
     }
 
     // Update is called once per frame
     void Update()
     {
         if (transform.position.x < screenBounds.x * -2)
         {
             Destroy(this.gameObject);
         }
     }
 }
 

And for Instantiating the prefab:

 public class spawnWind : MonoBehaviour
 {
     public GameObject windPrefab;
     public float respawnTime = 3.0f;
     private Vector2 screenBounds;
 
     // Start is called before the first frame update
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
         StartCoroutine(windWave());
     }
 
     void windSpawn()
     {
         GameObject a = Instantiate(windPrefab) as GameObject;
         a.transform.position = new Vector2(screenBounds.x * -2, -1.89f);
     }
 
     IEnumerator windWave()
     {
         while(true)
         {
             yield return new WaitForSeconds(respawnTime);
             windSpawn();
         }
     }
 }
 

Any help would be greatly appreciated.

Comment
Add comment · Show 3
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 KapoorArjun19 · May 16, 2020 at 02:52 PM 0
Share

The code looks fine, did you try Debug.Log() in 'windSpawn()'. I think the object is getting destroyed as soon as it is spawned.

avatar image hosterweis KapoorArjun19 · May 16, 2020 at 03:00 PM 0
Share

That was the problem! Thanks so much

avatar image ADiSiN hosterweis · May 16, 2020 at 04:21 PM 0
Share

Great that it solved your issue.

Also you can use pooler ins$$anonymous$$d of instantiation, so ins$$anonymous$$d of destroying and instantiating you will simply SetActive(false), reposition, SetActive(true). It's also better choice perfomance-wise, because you reduce Destroy and Instantiate calls overall, but that's just for information of another approach option.


Good luck in your project.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

227 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 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 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

Why is it important to create an empty gameobject for my prefabs? 0 Answers

How to instantiate a empty gameobject prefab 1 Answer

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Disable/Enable game object by clicking instantiated prefab during runtime 0 Answers

What does "Couldn't find matching instance in prefab" mean? 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