Answer the question
In order to leave comments, you need to log in
Why doesn't null work in Unity?
In code:
public Text infoObjectText;
private void Update()
{
float dis = 3.5f;
RaycastHit hit;
if (Physics.Raycast(playerCamera.GetComponent<Camera>().transform.position, playerCamera.GetComponent<Camera>().transform.forward, out hit, dis))
{
InfoObject infoObject = hit.transform.GetComponent<InfoObject>();
if (infoObject != null)
{
infoObjectText.enabled = true;
}
else if (infoObject == null)
{
infoObjectText.enabled = false;
}
}
}
else if (infoObject == null)
{
infoObjectText.enabled = false;
}
Answer the question
In order to leave comments, you need to log in
At you probably the component does not become null. Try another way.
PS: The code can be simplified toinfoObjectText.enabled = infoObject != null;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question