简体   繁体   English

当游戏对象被销毁时出现 MissingReferenceException

[英]MissingReferenceException when gameObject has been destroyed

I'm trying to learn how to make camera follow player in Unity.我正在尝试学习如何在 Unity 中让相机跟随玩家。 everything went fine.一切顺利。 the scripts works, no problem with the game, it just when my Character(Player) dead(destroyed), the console keep updating this error message脚本有效,游戏没问题,就在我的角色(玩家)死亡(被摧毁)时,控制台不断更新此错误消息

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. MissingReferenceException:“转换”类型的 object 已被销毁,但您仍在尝试访问它。 Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at <f53c831f77784ef08ef348217b5117fa>:0) CameraClamp.Update () (at Assets/Scripts/CameraClamp.cs:12)你的脚本应该检查它是否是 null 或者你不应该破坏 object。UnityEngine.Transform.get_position () (at <f53c831f77784ef08ef348217b5117fa>:0) CameraClamp.Update () (at Assets/Scripts/CameraClamp.cs:12)

and this还有这个

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. MissingReferenceException:“GameObject”类型的 object 已被销毁,但您仍在尝试访问它。 Your script should either check if it is null or you should not destroy the object. CameraFollow.FixedUpdate () (at Assets/Scripts/CameraFollow.cs:25)你的脚本应该检查它是否是 null 或者你不应该破坏 object.CameraFollow.FixedUpdate () (at Assets/Scripts/CameraFollow.cs:25)

and here's the scripts这是脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{

    public GameObject FollowObject;
    public Vector2 followOffset;
    public float speed = 3f;

    private Rigidbody2D rb;
    private Vector2 threshold;
    // Start is called before the first frame update
    void Start()
    {
     threshold = calculateThreshold();
     rb = FollowObject.GetComponent<Rigidbody2D>();
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector2 follow = FollowObject.transform.position;
        float xDifference = Vector2.Distance(Vector2.right * transform.position.x, Vector2.right * follow.x);
        float yDifference = Vector2.Distance(Vector2.up * transform.position.y, Vector2.up * follow.y);

        Vector3 newPosition = transform.position;
        if(Mathf.Abs(xDifference) >= threshold.x)
        {
            newPosition.x = follow.x;
        }
        if(Mathf.Abs(yDifference) >= threshold.y)
        {
            newPosition.y = follow.y;
        }

        float moveSpeed =  rb.velocity.magnitude > speed ? rb.velocity.magnitude : speed;
        transform.position = Vector3.MoveTowards(transform.position, newPosition, moveSpeed * Time.deltaTime);
    }

    private Vector3 calculateThreshold(){
        Rect aspect =  Camera.main.pixelRect;
        Vector2 t = new Vector2 (Camera.main.orthographicSize * aspect.width / aspect.height, Camera.main.orthographicSize);
        t.x -= followOffset.x;
        t.y -= followOffset.y;
        return t;
    }
    private void OnDrawGizmos() {
        Gizmos.color = Color.blue;
        Vector2 border = calculateThreshold();
        Gizmos.DrawWireCube(transform.position, new Vector3(border.x * 2, border.y * 2, 1));
        
    }
}

How to get rid of the error message?如何摆脱错误信息?

There's two reasonable solutions有两个合理的解决方案

  1. When you delete the player, you should delete this script too.当你删除播放器时,你也应该删除这个脚本。 This could be achieved by deleting the first parent of both FollowObject, CameraFollow, and Camera lamp.这可以通过删除 FollowObject、CameraFollow 和 Camera lamp 的第一个父对象来实现。 If they don't have a common parent, consider refactoring it so they do如果他们没有共同的父母,请考虑重构它以便他们这样做

  2. Check for null before accessing the transform在访问转换之前检查 null

    void FixedUpdate() { if(FollowObject.transform == null) { Delete(this); void FixedUpdate() { if(FollowObject.transform == null) { 删除(this); } } } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 MissingReferenceException:“GameObject”类型的 object 已被破坏,但您仍在尝试访问它 - Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed 有没有办法检查游戏对象是否已被销毁? - Is there a way to check if a GameObject has been destroyed? MissingReferenceException:类型为“攻击者”的对象已被破坏,但您仍在尝试访问它 - MissingReferenceException: The object of type 'Attacker' has been destroyed but you are still trying to access it 如何从已被破坏的外部GameObject执行协程 - How to execute Coroutine from external GameObject which has been destroyed “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it Brackeys 2d platformer GameObject&#39;已被破坏,但您仍在尝试访问它 - Brackeys 2d platformer GameObject' has been destroyed but you are still trying to access it 如何修复 Unity 中的“&#39;GameObject&#39; 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity The object of type 'GameObject' has been destroyed but you are still trying to access it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM