-1

I am learning to use Unity, I am currently doing the "Personal 3D Gallery Project" , here is the link if you want to take a look at it: https://learn.unity.com/project/create-a-personal-3d-gallery-project-with-unity?uv=2019.4

However, instead of displaying works of art in my gallery, I am displaying planets and my objective is that when the player approaches one of the planets, a text with information about that given planet pops out. In order to this, I am using the proximity script provided in the Unity website with a few changes. Everything seems to be working fine but when I approach the planets the text does not show and I keep receiving this error:

NullReferenceException: Object reference not set to an instance of an object
Proximity.Start () (at Assets/Scripts/Proximity.cs:32)

Apparently the problem is on line 32 but I can't figure out what's causing it. I am going to be attaching images for a better understating of the problem and my code.

I would really appreciate your help since this is for a final project, thank you so much in advance.

Here is my code:

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


public class Proximity : MonoBehaviour
{
    public string newTitle;
    public string newNumber;
    public string newInfo;
    private Transform other;
    private Text myTitle;
    private Text myNumber;
    private Text myInfo;
    private float dist;
    private GameObject player;
    private GameObject message1;
    private GameObject message2;
    private GameObject message3;
    private bool check;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindWithTag("Player");
        other = player.GetComponent<Transform>();
        message1 = GameObject.FindWithTag("PlanetTitle");
        message2 = GameObject.FindWithTag("PlanetNumber");
        message3 = GameObject.FindWithTag("PlanetInfo");
        myTitle = message1.GetComponent<Text>();
        myTitle.text = "";
        myNumber = message2.GetComponent<Text>();
        myNumber.text = "";
        myInfo = message3.GetComponent<Text>();
        myInfo.text = "";
        check = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (other)
        {
            dist = Vector3.Distance(transform.position, other.position);
            print("Distance to player: " + dist);
            if (dist < 4)
            {
                myTitle.text = newTitle;
                myNumber.text = newNumber;
                myInfo.text = newInfo;
                check = true;
            }
            if (dist > 4 && check == true)
            {
                Start();
            }
        }
    }
}

This is what I'm trying to do

But this is how it looks when I approach the planet (or moon in this case)

Here is my inspector, just in case the issue could be there

I checked my code multiple times and compared it to the original, I think I made all the necessary changes but clearly there is something wrong with it.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Estefania, all of your GetComponents should generally be placed inside the Awake method not start. myTitle is not defined at the time that you are trying assign the empty string to it. – Classified Dec 12 '22 at 05:29
  • @Classified `should generally be placed inside the Awake method not start.` ... why? Makes no difference at all in this case here ... – derHugo Dec 12 '22 at 07:26
  • Sounds to me like the `message1` does not have a component `Text` attached to it so `message1.GetComponent()` returns `null` .... I suggest you debug your code and see which object(s) are actually returned by your `FindWithTag` calls ... maybe you tagged the wrong object ;) – derHugo Dec 12 '22 at 07:27

1 Answers1

0

myTitle is not initialised before you are setting the property text on it. Initialise it by assigning the required components in Awake.

void Awake()
{
    player = GameObject.FindWithTag("Player");
    other = player.GetComponent<Transform>();
    message1 = GameObject.FindWithTag("PlanetTitle");
    message2 = GameObject.FindWithTag("PlanetNumber");
    message3 = GameObject.FindWithTag("PlanetInfo");
    myTitle = message1.GetComponent<Text>();        
    myNumber = message2.GetComponent<Text>();
    myInfo = message3.GetComponent<Text>();
}

void Start()
{       
    myTitle.text = "";
    myNumber.text = "";
    myInfo.text = "";
    check = false;
}
Classified
  • 232
  • 1
  • 6
  • How is that different from what OP already does? `myTitle = message1.GetComponent(); myTitle.text = "";` – derHugo Dec 12 '22 at 07:22
  • The problem was among those lines, there was an issue with the GetComponent, my professor explained to me that I was using TextMeshPro and the demo was using plain Text, so the script was looking for the generic text Object. Thanks for trying to help me! – Estefania Cenci Dec 13 '22 at 18:21