Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Respawn errors

Discussion in 'Scripting' started by Muku, Apr 25, 2014.

  1. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    Alright, I'm new to scripting so I'm still learning the terms.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var Player : GameObject;
    4. var spawnPoint : Transform;
    5.  
    6. function OnTriggerEnter(other : Collider){
    7.     if(other.tag == "Player"){
    8.         Destroy(other.gameObject);
    9.         var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
    10.         var sf = Camera.main.GetComponent(CameraFollow);
    11.         sf.target = P.transform;
    12.     }else{
    13.         Destroy(other.gameObject);
    14.     }
    15. }
    That is code for respawning but when the player dies. When he respawn at the "SpawnPoint" the FPS controller and Character Motor scripts are turned off.
    Also when the character respawns for a second time I get an error saying:

    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Missing reference exception is possibly from your camera script. Double click the error message and it should bring you to the code that is throwing it.

    If you destroy the gameobject, then the camera tries to access the transform before you've assigned it a new one, it will throw MissingReferenceException. There is a very tiny gap between destroying the old player and assigning the new one, so the error could be thrown there, though it might not be consistent.

    However if the error is ALWAYS thrown on respawn 2, but NEVER thrown on respawn 1, that suggests to me that maybe the respawned player does not have a 'Player' tag, so it is being destroyed without spawning a new one, which throws off the camera script.

    If you are going to be destroying gameobjects that other scripts reference, make sure those scripts have checks in place to ensure they don't have null references, or manually unassign them before destroying the referenced object.


    As for unchecked components, I would need more information on the Player Prefab - make sure the prefab itself (and not just the instance in the scene) is set up properly (this includes checking that 'Player' tag).
     
  3. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    Alright, a lot of that make sense as far as I can understand but I'm getting two errors total. Each of them is on the second respawn everytime and the prefab for the player is tagged as "Player" which is the one I brought into the scene.

    The second error is part of the "transform" function in my CameraFollow script but I assume that's just because it can't find the player anymore.

    The initial error from the first post links my to whatever kills my player, either the Enemy or the Killbox under the map. The line it links to is the instatiate player line. Am i missing a piece of code from how the player respawns after he gets destroyed?

    Edit: Also the I am not able to see the second respawned player for whether they have a "Player" tag or not as it doesn't create another instance. The first respawn however does have the "Player" tag but it shuts off it's controller scripts that allow me to move them. Also the code below is the code for the enemy.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var fall : boolean;
    4. var Player : GameObject;
    5. var spawnPoint : Transform;
    6. var stomp : boolean;
    7.  
    8. function Update () {
    9.     if(stomp){
    10.         transform.position.z = 4;
    11.         transform.localScale.y /= 2;
    12.         fall = true;
    13.         gameObject.GetComponent(PlatformMover).Step = 0.0; 
    14.         stomp = false;     
    15.     }
    16.     if(fall){
    17.         transform.position.y -= 0.05;
    18.     }
    19.     if(transform.position.y < -25){
    20.         Destroy(gameObject);
    21.     }
    22. }
    23.  
    24. function OnTriggerEnter(other : Collider){
    25.     if(!stomp){
    26.         if(other.tag == "Player"){
    27.             Destroy(other.gameObject);
    28.             var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
    29.             var sf = Camera.main.GetComponent(CameraFollow);
    30.             sf.target = P.transform;
    31.         }
    32.     }
    33. }
     
    Last edited: Apr 28, 2014
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    So if I am reading this correctly, you are getting the exception : MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

    And it points to the line:

    Code (csharp):
    1.  var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);  
    Are you destroying 'spawnPoint' at any point in time? spawnPoint is a Transform object you are trying to access when the error occurs. Check that it still exists when the error is being thrown.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Have you assigned an object to the player variable which exists in the scene or a prefab?

    If you plan to instantiate the player like this, make sure it's a prefab that you instantiate. Otherwise, a clone of the object in the scene will be instantiated once and if you destroyed it and afterwards try to instantiate it again, it will not exist anymore.
    Check the inspector and have a look at what exactly happens when you destroyed your player for the first time. If you do not see any changes in the inspector, click somewhere into the inspector or click on something different and once again on the gameobject which holds your instantiating script.

    As a result, when you destroyed the original object once, it's transform cannot be accessed anymore which is represented by the exception about the gameobject and transform.

    If you still want to go with this code, try to replace your local variable p with the Player variable of your class, this way, the newly instantiated clone will be your new template for the next instantiation.
     
    Last edited: Apr 28, 2014
  6. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    Thank you both for your help. It's funny how such a minor mishap causes an annoying issue.
    It was the prefab that needed to be linked to the scripts in the inspector instead of the player in the game. As Suddoha said it would make a clone of the player in the hierarchy and then be unable to transform after.

    Thanks guys!