I have an object, called ‘Point’, that is set to ‘is trigger’, and has a script called ‘GetPoint’ that will cause it to destroy itself and change a float, ‘points’, by +1 when the player comes in contact with it. I am trying to display the points in a ui - text script called ‘Score’. It seems to work, but after the player touches the object and the score is displayed in the console (which is how I set it while trying to get it to work) the following error message appears.
NullReferenceException: Object reference not set to an instance of an object
Score.Update () (at Assets/Score.cs:18)
GetPoint script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetPoint : MonoBehaviour {
public GameObject pickupEffect;
public float points = 0f;
void OnTriggerEnter (Collider other)
{
if (other.CompareTag("Player"))
{
Pickup();
}
}
void Pickup()
{
//Instantiate(pickupEffect, transform.position, transform.rotation); ignore this
points += 1f;
Debug.Log(points);
Destroy(gameObject);
}
}
Score script:
using UnityEngine;
public class Score : MonoBehaviour
{
GameObject Point;
// Update is called once per frame
void Update()
{
GameObject Point = GameObject.Find("Point");
GetPoint GetPoint = Point.GetComponent<GetPoint>();
Debug.Log(GetPoint.points);
}
}
Please help!