Search Unity

How do Spawn the Object"Player" after I destroyed it?

Discussion in 'Scripting' started by yaIi, Jun 8, 2015.

  1. yaIi

    yaIi

    Joined:
    Jun 8, 2015
    Posts:
    7
    (Beginner, 3D Project) Well that's what I have so far:
    1. using UnityEngine;
    2. using System.Collections;

    3. public class Obstacles : MonoBehaviour {
    4. // Use this for initialization
    5. void Start () {
    6. }
    7. // Update is called once per frame
    8. void Update () {
    9. }
    10. void OnCollisionEnter(Collision collision){
    11. if (collision.gameObject.name == "Player") {
    12. Destroy(collision.gameObject);
    13. }

    14. }
    15. }
    Plan: After it's gone, i'd like to wait for 2 Seconds and then it should spawn again at 0,0,0
    I'm coding since yesterday, so it could be simple af, but still thanks for the big help!
     
    Bacon_Studio likes this.
  2. NeilM0

    NeilM0

    Joined:
    Mar 31, 2009
    Posts:
    135
    The Invoke command will run a function after a set amount of time.

    Code (CSharp):
    1. Invoke("DelayedSpawn", 2.5f);
    2. ...
    3.  
    4. private void DelayedSpawn()
    5. {
    6.    GameObject player = Instantiate<GameObject>(mPlayerPrefab);
    7. }
    8.  
     
  3. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    You'll want to look up a tutorial on creating and instantiating prefabs. I would explain it here, but it would take a lot of time and if you could find a video you'll understand it quickly and easily.
     
    yaIi likes this.
  4. yaIi

    yaIi

    Joined:
    Jun 8, 2015
    Posts:
    7
    Thx there! Is your "Get Me Started" maybe Game something for me?
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Im not sure if there is a way to get a prefab from a gameobject, so you would have to know it before hand or make your own list of prefabs and gameobject references.
    However, maybe instead of destroying, you can get away with just disabling and reenabling.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Obstacles : MonoBehaviour
    5. {
    6.     void OnCollisionEnter(Collision collision)
    7.     {
    8.         if (collision.gameObject.name == "Player")
    9.         {
    10.             collision.gameObject.SetActive(false);
    11.             StartCoroutine(Respawn(collision.gameObject));
    12.         }
    13.     }
    14.  
    15.     IEnumerator Respawn(GameObject gObj)
    16.     {
    17.         yield return new WaitForSeconds(2);
    18.         gObj.transform.position = Vector3.zero;
    19.         gObj.SetActive(true);
    20.     }
    21. }
     
  6. yaIi

    yaIi

    Joined:
    Jun 8, 2015
    Posts:
    7
    Holy S*** that works perfectly! Thx so much ^^
     
    HiddenMonk likes this.
  7. NeilM0

    NeilM0

    Joined:
    Mar 31, 2009
    Posts:
    135
    "Get Me Started" wouldn't help in this specific case. For example I don't have anything in there that tells you how to spawn a character (That's more left up to the individual programmer because every game is different). But it is a basic framework to setup menus, localization, etc. so it would help with that part of your game.
     
    yaIi likes this.
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If you want to destroy your character, then you would probably want to use its prefab to bring it back

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Obstacles : MonoBehaviour
    5. {
    6.     void OnCollisionEnter(Collision collision)
    7.     {
    8.         if (collision.gameObject.name == "Player")
    9.         {
    10.             GameObject prefab = collision.gameObject.GetComponent<PlayerPrefab>().prefab;
    11.  
    12.             Destroy(collision.gameObject);
    13.  
    14.             StartCoroutine(Respawn(prefab));
    15.         }
    16.     }
    17.  
    18.     IEnumerator Respawn(GameObject prefab)
    19.     {
    20.         yield return new WaitForSeconds(2);
    21.         Instantiate(prefab); //defaults to position Vector3.zero
    22.     }
    23. }
    You put the script below on the player, and put its prefab into the slot on the inspector
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerPrefab : MonoBehaviour
    5. {
    6.     public GameObject prefab; //assign this in the inspector
    7. }

    Here is a unity tutorial on instantiating
    https://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate
     
    yaIi likes this.
  9. Admiral-Jason

    Admiral-Jason

    Joined:
    Oct 14, 2018
    Posts:
    28
    Would there be a reason why the lines of code after the yield line are not executed? In the code I've created I've integrated these suggestions and the game waits much longer than two seconds. In fact it stops executing the Respawn method at the yield statement.
     
    Last edited: Mar 16, 2019
  10. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Maybe the object that is running the coroutine got disabled. A disabled gameobject cant run coroutines. Maybe you have this script on two objects that collided with eachother, so one of them disabled the other and now the other cant disable the first?
     
    Admiral-Jason likes this.
  11. Admiral-Jason

    Admiral-Jason

    Joined:
    Oct 14, 2018
    Posts:
    28
    No, I verified that this script is only running on one of the objects, not both. Also, the other coroutine has to do with spawning hazards.
     
  12. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Not sure then, put a OnDisable OnDestroy with some debug logs on the script to make sure its not being deactivated, make sure no StopAllCoroutine calls are made, make sure Time.timeScale is not 0, put a debug log inside the coroutine before the yield and after the yield. You probably did a lot of this already, but I dont really know what else it can be ^^.
    Make sure you are doing StartCoroutine(Respawn(collision.gameObject)); and not StartCoroutine(Respawn(gameObject)); on accident.
     
    Admiral-Jason likes this.
  13. Admiral-Jason

    Admiral-Jason

    Joined:
    Oct 14, 2018
    Posts:
    28
    It was in an "OnTrigger" call. Something I figured out was that it seemed to have something to do with running two coroutines in two scripts. I don't understand why, but that's what I found. So I came up with a workaround. I still have the player object deactivate in the script with on trigger in it, but to get around the issue, I have the player be reactivated at the beginning of the next wave of hazards (which is the first coroutine). I do appreciate everyone's help though! :D
     
    HiddenMonk likes this.