C# 错误:";NullReferenceException:对象引用未设置为对象的实例;对于Unity中一个对象上的两个脚本

C# 错误:";NullReferenceException:对象引用未设置为对象的实例;对于Unity中一个对象上的两个脚本,c#,unity3d,C#,Unity3d,有可能这是一个重复,但我已经尝试了,并试图让它的工作,它可能是一些简单的东西 我试图让两个脚本在一个对象上交互。我想要一个基本的通用物理脚本,它将处理我所有对象的所有交互 脚本1: // Simple Player1 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player1 : MonoBehaviour { Physics physics_

有可能这是一个重复,但我已经尝试了,并试图让它的工作,它可能是一些简单的东西

我试图让两个脚本在一个对象上交互。我想要一个基本的通用物理脚本,它将处理我所有对象的所有交互

脚本1:

// Simple Player1

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


public class Player1 : MonoBehaviour
{
    Physics physics_script;

    void Start(){
        physics_script = gameObject.GetComponent<Physics>();
    }

    // Update is called once per frame
    void Update() {
        physics_script.helloWorldFromAnotherScript();
    }
}
编辑: 我很确定问题出在这里:

但我不能改变它来拯救我的生命。

它应该会起作用。 您确定已将两个脚本都添加到游戏对象中吗?在这张图片上,我只看到了player1,我怀疑是否添加了物理元素。 在其他地方,您可以将physics_脚本公开或序列化(使用[SerializeField]),并且您可以在编辑器中将脚本拖放到formfield。这允许您在开始时不使用GetComponent。 如果不使用启动和更新方法,请记住删除它们

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

public class Physics : MonoBehaviour
{
    void Start(){
    }

    void Update() {   
    }

    public void helloWorldFromAnotherScript()
    {
        Debug.Log("Hello world from player");
    }
}