Help with coding

Updated on August 15, 2018 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on August 15, 2018

Problem:

So I am currently playing around with the “Cube Game project” and since I am a hardcore noob I have no idea how to fix this. So the thing is. I have two scripts one for the Camera to follow the player(FollowPlayer) and another one for dealing with the collision(PlayerCollision)
The problem I have is when my player hits an obstacle with the tag “obstacle” I have set it to kill my player and display a quick effect, but when my player dies it comes up with multiple errors. I am aware of the problem but I have no idea how to disable the camera if my player hits the obstacle.

ERROR:

MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position ()
FollowPlayer.Update () (at Assets/HowToMakeAVideoGame/Assets/Scripts/FollowPlayer.cs:11)

FollowPlayer:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

public Transform player; 
public Vector3 offset; 

void Update ()
{
transform.position = player.position + offset;
}
}

PlayerCollision:

using UnityEngine;

public class PlayerCollision : MonoBehaviour {

public GameObject deathEffect;
public PlayerMovement movement;

public void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Obstacle”)
{
movement.enabled = false;
GameObject effectIns = (GameObject)Instantiate(deathEffect, transform.position, transform.rotation);
Destroy(effectIns, 2f);
Destroy(gameObject);
FindObjectOfType<GameManager>().EndGame();
}
}

}

  • Liked by
Reply
0 on August 15, 2018

Exactly what the error says.

The object of type ‘Transform’ has been destroyed but you are still trying to access it.

The Transform it’s talking about is the player, which you destroyed, but you’re still trying to use it.

 

And exactly what it says to do, is how you should fix it.

Your script should either check if it is null or you should not destroy the object.

You can check if it’s null and break out of the update loop with:

if(player == null) return;
  • Liked by
Reply
Cancel
0 on August 15, 2018

@Mouledoux,

Thank you very much for the assistance!

Since I’m a beginner and probably need a lot of help there is a high chance we’ll meet again XD

  • Liked by
Reply
Cancel