John R
John R

Reputation: 2064

How to solve these errors in unity?

I am newbie in unity. And I am creating 2-d game by taking help from some links. My game working fine. But when character destroyed game start from start point as usual but its pause there. And show some errors I dont know how to solve these errors.

PlayerRespawn.js-

var Player : GameObject;
var SpawnPoint: Transform;

function OnTriggerEnter(other: Collider) {
Destroy(other.gameObject);
var P: GameObject= Instantiate(Player,SpawnPoint.position,Quaternion.identity);

var sf = Camera.main.GetComponent(SmoothFollow2);
sf.target=P.transform;
}

SmoothFollow2.js-

var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
var lockRotation : boolean;

function Update () {
    var wantedPosition = target.TransformPoint(0, height, -distance);
    transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

    if (smoothRotation) {
        var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
        transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
    }


    else transform.LookAt (target, target.up);

    if (lockRotation){
    transform.localRotation=Quaternion.EulerAngles(0,0,0);
    }
}

And the errors I am getting are-

NullReferenceException
PlayerRespawn.OnTriggerEnter (UnityEngine.Collider other) (at Assets/MyScripts/PlayerRespawn.js:8)
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

Upvotes: 1

Views: 1297

Answers (3)

Graeme MacDonald
Graeme MacDonald

Reputation: 1

i solved the issue by making sure that the main camera was attached to the player as a child before creating the Player Prefab so that when it re spawns it has the main camera

hope this helps you solve your issue

"OK for added value i know the tutorial your working with and to help in understanding my post above what you need to do is, before making your prefab of the player(first person controller) in your hierarchy in the unity editor drag and drop the main camera onto the (first person controller) then make that your prefab of Player. – "

Upvotes: -1

TutiBueno
TutiBueno

Reputation: 21

I would not destroy the player. I would just disable and then enable it again in another defined position, or the initial position.

By doing this your camera, which is following your player by the var "target" will always have the referenced object and will stop throwing the error for you.

to disable a game object:

gameObject.SetActive = false;

To enable it again:

gameObject.SetActive = true;

I hope that helps.

Upvotes: 0

Steven Mills
Steven Mills

Reputation: 2381

Considering the line that throws the error is:

var sf = Camera.main.GetComponent(SmoothFollow2);

It sounds like perhaps your camera has been destroyed. One way to check for this is to stick a Debug message in your trigger, to see what exactly is being destroyed. Try putting Debug.LogWarning(other.gameObject.name); at the beginning of your OnTriggerEnter(other: Collider) function. If you see the camera name show up as a warning, then you know this is the case.

You can prevent this by doing:

if(other.gameObject.name == Camera.main.gameObject.name)
    return;

A better solution, if there's only a limited range of items that need to be destroyed, is to allow only those to continue through the OnTriggerEnter.

Upvotes: 3

Related Questions