让对象移动到玩家

时间:2016-03-30 18:55:16

标签: c# unity3d

我一直在网上寻找我的问题的答案。我找不到任何有效的东西,所以我决定将它发布在这里。

我一直收到错误:"Object reference not set to an instance of an object"

双击它时会发送给我:

`transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 3f);  //move towards character`

我创建了随机网格(小行星,这是有效的),它们应该向播放器移动(错误!)。

为了让小行星追逐我的播放器,我在顶部添加了以下变量:

 bool hitPlayer = false;
 GameObject player;

我在启动功能中添加了这个:

player = GameObject.FindGameObjectWithTag("Player");

我在更新功能中添加了这个:

transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 3f);  //move towards character

这是我的完整代码:

using UnityEngine;
using System.Collections;

public class RandomAsteroid : MonoBehaviour
{

    public Material material;
    private Material collectedMaterial;
    private MeshRenderer meshRenderer;
    private Collider collider;


    bool hitPlayer = false;
    GameObject player;


    // Use this for initialization

    void Start()
    {
        collectedMaterial = (Material) Resources.Load("sun", typeof(Material));

        Vector3 v0 = new Vector3(Random.Range(1f,5f), 1, Random.Range(1f,5f));
        Vector3 v1 = new Vector3(Random.Range(1f,5f), 1, Random.Range(-1f,-5f));
        Vector3 v2 = new Vector3(Random.Range(-1f,-5f), 1, Random.Range(-1f,-5f));
        Vector3 v3 = new Vector3(Random.Range(-1f,-5f), 1, Random.Range(1f,5f));

        Vector3 v4 = new Vector3(Random.Range(1f,5f), -1, Random.Range(1f,5f));
        Vector3 v5 = new Vector3(Random.Range(1f,5f), -1, Random.Range(-1f,-5f));
        Vector3 v6 = new Vector3(Random.Range(-1f, -5f), -1, Random.Range(-1f, -5f));
        Vector3 v7 = new Vector3(Random.Range(-1f,-5f), -1, Random.Range(1f,5f));

        MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
        meshRenderer = gameObject.AddComponent<MeshRenderer>();


        meshRenderer.material = material;

        Mesh mesh = meshFilter.mesh;

        mesh.Clear();

        Vector3[] duplicateVertices = new Vector3[]
            {
                //top 
                v0, //0
                v1, //1
                v2, //2
                v3, //3

                //front
                v4, //4
                v0, //5
                v3, //6
                v7, //7

                //left
                v5, //8
                v1, //9
                v0, //10
                v4, //11

                //back
                v6, //12
                v2, //13
                v1, //14
                v5, //15

                //right
                v7, //16
                v3, //17
                v2, //18
                v6, //19

                //bottom
                v5, //20
                v4, //21
                v7, //22
                v6 //23
            };

        mesh.vertices = duplicateVertices;


        int[] triangles = new int[]
            {
                //top
                0, 1, 2,
                2, 3, 0,

                //front
                4, 5, 6,
                6, 7, 4,

                //left
                8, 9, 10,
                10, 11, 8,

                //back
                12, 13, 14,
                14, 15, 12,

                //right
                16, 17, 18,


 18, 19, 16,

            //bottom
            20, 21, 22,
            22, 23, 20


        };
    mesh.triangles = triangles;
    Collider collider = gameObject.AddComponent<MeshCollider> ();

    mesh.RecalculateNormals();
    mesh.RecalculateBounds ();
    mesh.Optimize();

    player = GameObject.FindGameObjectWithTag("Player");
}

// Update is called once per frame
void Update()
{
    if (!hitPlayer)
    {
        transform.position = Vector3.MoveTowards(transform.position, player.transform.position, 3f);  //move towards character
    }
}

void OnCollisionEnter(Collision collision){

    if (collision.gameObject.name == "Player")
    {
        meshRenderer.material = collectedMaterial;
    }

}

}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您的问题可能来自player = GameObject.FindGameObjectWithTag("Player"); 如果您没有标记为“播放器”的GameOBject,那么这就是问题所在。标记与命名不同。如果您的gameObject名为“播放器”,则使用GameObject.Find()代替GameObject.FindGameObjectWithTag()

所以替换

player = GameObject.FindGameObjectWithTag("Player")

player = GameObject.Find("Player");