Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 no666mi · Apr 09, 2021 at 04:36 AM · scripting problem2d gameunity 2d

How can i spawn and Despawn for a specific time using this script?

Hi I want to spawn a prefab for 10 seconds before destroying it and respawning it again. If player captures it than i want to immediately spawn a new one.

I tried putting script on the prefab collider of object so that when it collides with the player it immediately spawns a new one but that doesnt work.

public GameObject point;

 private Vector2 screenBounds;

 public float respawnTime = 10f;

 void Start()
 {
     screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
     spawnStar();
     StartCoroutine(starWave());

 }
   
 public void spawnStar()
 {
     Debug.Log("Yeah it works");
     GameObject star;
     star = Instantiate(point) as GameObject; 
     star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x)*0.3f, Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
 }


 IEnumerator starWave()
 {
     while (true)
     {
         yield return new WaitForSeconds(respawnTime);
         spawnStar();
         Destroy(point);

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by singhlaxman9761 · Apr 09, 2021 at 05:04 AM

This is main script where first prefab will instantiate

 public class Test : MonoBehaviour
     {
           public GameObject prefab;
           private Vector2 screenBounds;
           public float respawnTime = 10f;
 
          void Start(){
               screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
             spawnStar();
             spawnStar();
          }
     
          public void spawnStar()
          {
              Debug.Log("First Init");
             GameObject star;
             star = Instantiate(prefab) as GameObject;
             star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x) * 0.3f,
             Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
          }
     }
 

Attech PrefabTest.cs on prefab and assign prefab it

 public class PrefabTest : MonoBehaviour
 {
     public GameObject prefab;
     private Vector2 screenBounds;
     public float respawnTime = 10f;
 
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
         Invoke("spawnStar", respawnTime);
     }    
 
     public void spawnStar()
     {
         Debug.Log("Yeah it works");
         GameObject star;
         star = Instantiate(prefab) as GameObject;
         star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x) * 0.3f,
         Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
 
         DestroyImmediate(gameObject);
     }
 }



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 no666mi · Apr 09, 2021 at 03:45 PM 0
Share

Thank you I will try this

avatar image
0

Answer by Rapizer · Apr 09, 2021 at 05:07 AM

I'm not sure what you mean, the script above only spawns point every 10 seconds, nothing in the script tells it to spawn a new star on collision with the player. Also, which object are you putting the above script in exactly?


Also, I think the reason the above script isn't working is because you shouldn't be destroying "point", you should be destroying "star". (untested code so may not work, if it doesn't, tell me the error message it's getting)

 private Vector2 screenBounds;
 public float respawnTime = 10f;
 private GameObject star;
 
  void Start()
  {
      screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
      spawnStar();
      StartCoroutine(starWave());
  }
    
  public void spawnStar()
  {
      Debug.Log("Yeah it works");
      star = Instantiate(point) as GameObject; 
      star.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x)*0.3f, Random.Range(-screenBounds.y, screenBounds.y) * 0.3f);
  }
  IEnumerator starWave()
  {
      while (true)
      {
          yield return new WaitForSeconds(respawnTime);
          Destroy(star); // destroy star not point
          spawnStar();
      }
  }
Comment
Add comment · Show 2 · 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 no666mi · Apr 09, 2021 at 03:46 PM 0
Share

This works but how can i spawn it immediately after player collides with it?

avatar image no666mi · Apr 09, 2021 at 04:53 PM 0
Share

above script is on a empty gameobject

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

247 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers

Mouse Click Walk to Idle Animations 0 Answers

Unity Admob didint show in android device 0 Answers

AI script that follows player in a distance 1 Answer

Cannot create mesh from code,Cannot use Script to Generate 2d mesh 0 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