0

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?

Meindert Stijfhals
  • 355
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jason Watkins Mar 30 '16 at 18:56
  • It may be as simple as `gameObject.FindGameObjectWithTag("Player");` instead of `GameObject.FindGameObjectWithTag("Player");` ... use the debugger to find out. – Filburt Mar 30 '16 at 19:00
  • @Filburt that doesn't work at all :( – Meindert Stijfhals Mar 30 '16 at 19:11
  • That was a mere guess at why maybe your `player` is `null`. It's also not clear from the code shown, where `transform` is declared and initialized. Put a breakpoint on the line your getting to error and inspect your objects. – Filburt Mar 30 '16 at 19:17
  • Well, obviously `player` is `null`. It is `GameObject.FindGameObjectWithTag()`. If you don't find anything, have a look if you have any object with that tag. – Gunnar B. Mar 30 '16 at 19:18
  • I have an empty GameObject named Player, in that empty GameObject I have a spaceship – Meindert Stijfhals Mar 30 '16 at 19:25
  • @MeindertStijfhals Well, that is not what it should be. Have a look at the answer down below: The name of the object doesn't matter for this, the tag does. – Gunnar B. Mar 30 '16 at 20:17

1 Answers1

1

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");
Programmer
  • 121,791
  • 22
  • 236
  • 328