Warning on null-check (warning CS0472)?

I get a warning (warning CS0472) when I check if a value is null. This is the code I’m using:

BuildingSystem.cs:

using UnityEngine;
using System.Collections;

public class BuildingSystem : MonoBehaviour {
	
	private Vector3 point;
	private GameObject selectedObject;

	void Start () {

	}

	void Update () {
		if(SelectionSystem.getInWorldPoint() == null) {           // warning CS0472
			selectedObject = SelectionSystem.getSelection();
		} else {
			point = SelectionSystem.getInWorldPoint();
		}
	}
}

SelectionSystem.cs:

using UnityEngine;
using System.Collections;

public class SelectionSystem : MonoBehaviour {

	private static RaycastHit hit;
	private static GameObject selected = null;
	private static Vector3 inWorldPoint = new Vector3(0, 0, 0);

	void Start () {

	}

	void Update () {
		Ray ray = camera.ScreenPointToRay(Input.mousePosition);

		if(Physics.Raycast(ray, out hit, 500)) {
			if(Input.GetKeyDown(KeyCode.Mouse0)) {
				if(hit.collider.tag == "Selectable") {
					selected = hit.collider.gameObject;
					Debug.Log("Selected: " + selected.name);
				} else {
					selected = null;
					inWorldPoint = hit.point;
				}
			}
		}
	}

	public static GameObject getSelection() {
		return selected;
	}

	public static Vector3 getInWorldPoint() {
		return inWorldPoint;
	}
}

I get the warning on line 14 in BuildingSystem.cs (Commented in the code).
Warning text:

warning CS0472: The result of comparing value of type UnityEngine.Vector3' with null is false’

I’ve read this, but the value I’m getting an error for is actually changing. Why do I get that error???

/TheDDestroyer12

3 Answers

3

In your BuildingSystem.cs change your line with error to:

if(SelectionSystem.getInWorldPoint() == Vector3.zero) {

instead of null.

Thanks! I just found that out. Thanks everyone!

You are comparing a Vector3 with null. Vector3 is a non-Nullable type, which means it will never be null. The warning tells you that the result of the check will always be false, and is therefore redundant.

Thanks! As said above, I found out the answer. Thanks!

the returning value of this SelectionSystem.getInWorldPoint() wont be null in worst case scenario it will be zero

Thanks! That worked.