Meindert Stijfhals
Meindert Stijfhals

Reputation: 355

Make object move to player

I've been looking on the internet for the answer to my problem. I couldn't find anything that worked so I decided to post it here.

I keep getting the error: "Object reference not set to an instance of an object"

When doubleclicking it sends me to this:

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

I created random meshes (asteroids, this works) and they are supposed to move towards the Player (error!).

To make the Asteroids chase my Player, I added the following variables on top:

 bool hitPlayer = false;
 GameObject player;

I added this in the start function:

player = GameObject.FindGameObjectWithTag("Player");

and I added this in the update function:

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

Here is my complete code:

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;
    }

}

}

Can anyone help me please?

Upvotes: 0

Views: 221

Answers (1)

Programmer
Programmer

Reputation: 125275

Your problem is likely from player = GameObject.FindGameObjectWithTag("Player"); If you you don't have a GameOBject that is tagged as "Player" then that is the problem. Tag is different from naming. If your gameObject is named "Player" then use GameObject.Find() instead of GameObject.FindGameObjectWithTag().

So replace

player = GameObject.FindGameObjectWithTag("Player")

with

player = GameObject.Find("Player");

Upvotes: 1

Related Questions