H
H
Hixac2020-09-28 20:50:11
Unity
Hixac, 2020-09-28 20:50:11

NullReferenceException: Object reference not set to an instance of an object, why does it return null?

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

public class Sensor : MonoBehaviour
{
    void Start()
    {

    }
    void Update()
    {
        if (GetComponent<Fruit>().transform.position == GetComponent<Move>().transform.position)
        {
            Destroy(GetComponent<Fruit>().gameObject);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2020-09-28
@Hixac

Well, you do not have all three components on the object Sensor+ Fruit+ Move.
Then, even if they exist, their position will always be the same, because this is the same transform for all.
P.S. GetComponentin Update, yes, you should start with guides

D
DanielMcRon, 2020-09-28
@DanielMcRon

Where's the error? And I realized that the 2nd script knocks out an error on line 26

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question