NullreferenceException when checking if point is visible

162 views Asked by At

I know that there is a possible duplicate and I tried to find what causes my null, but maybe You can help me anyway. So I'm making a game in which I spawn randomly "PoliceMan" at six points defined as array in inspector. This is done periodically by coroutine. I tried to add a method for checking if spawn point is visible by camera. Here are my code snippets:

IEnumerator AddPoliceManCR()
{

    int counter = 0;
    yield return new WaitForSeconds(1.0f);
    while (counter < 10)
    {

        AddPoliceMan();
        counter++;
        yield return new WaitForSeconds(Random.Range(3.0f, 5.0f));
    }
    while (true)
    {
        AddPoliceMan();
        yield return new WaitForSeconds(Random.Range(0.5f, 3.0f));
    }

}


 void AddPoliceMan()
{
    GameObject policeManClone = (GameObject)policemanPool.getPooledPoliceman();
    policeManClone.transform.position = SetSpawnPosition();
    policeManClone.transform.LookAt(new Vector3(player.transform.position.x, 0, player.transform.position.z));

    policeManClone.tag = "PoliceMan";
    policeManClone.SetActive(true);
}



Vector3 SetSpawnPosition()
{
    Vector3 position;


    position = spawnPositions[Random.Range(0, 6)];
    Debug.Log(position.ToString());
    if (Camera.current.WorldToViewportPoint(position).x > 0f
        && Camera.current.WorldToViewportPoint(position).x < 1f
        && Camera.current.WorldToViewportPoint(position).y > 0f
        && Camera.current.WorldToViewportPoint(position).y < 1f
        && Camera.current.WorldToViewportPoint(position).z > 0f)
    {
        Debug.Log("Spawn point visible");
    }
    else
    {
        Debug.Log("Spawn point is NOT visible");
    }


    return position;

}

Coroutine is stated in Start(). This script and gameObject is not destroyed at any point. NullReferenceException is in line: if (Camera.current.WorldToViewportPoint(position).x > 0f Debug.Logs show correctly if points are visible or not, at least most of the time.

The problem is that this does not occur every time the SetSpawnPopsition() is called. It might be right away the game starts or after 20 calls of this method. Seems random. Also if point is initialized just two lines before why does the error show? If I set back line policeManClone.transform.position = SetSpawnPosition(); to policeManClone.transform.position = spawnPositions[Random.Range(0, 6)]; I do not have any errors.

The exact error is:

NullReferenceException: Object reference not set to an instance of an object GameControl.SetSpawnPosition () (at Assets/Scripts/GameControl.cs:140) GameControl.AddPoliceMan () (at Assets/Scripts/GameControl.cs:67) GameControl+c__Iterator0.MoveNext () (at Assets/Scripts/GameControl.cs:92) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

EDIT

Ok, I had idea that it might be the other thing. Looks like Camera.current is giving me null. After changing it to a public variable with main camera assigned in inspector this error seems to be corrected. Question is why is Camera.current causing null?

1

There are 1 answers

0
Programmer On BEST ANSWER

Ok, I had idea that it might be the other thing. Looks like Camera.current is giving me null. After changing it to a public variable with main camera assigned in inspector this error seems to be corrected. Question is why is Camera.current causing null?

Camera.current will be return if the Scene View does not have focus. You should be using Camera.main not Camera.current.

Also, before using Camera.main, make sure that the camera has the tag MainCamera. This is done by default when new scene is created but you have to verify this or you will get the null exception again.