0

I am trying to make a VR game for google cardboard, and i am trying to set the FOV of a camera after 2 seconds, however i get the error:

"NullReferenceException: Object reference not set to an instance of an object CameraFOV.Start"

using UnityEngine;
using System.Collections;
public class CameraFOV : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        System.Threading.Thread.Sleep(2000);
        Camera.current.fieldOfView = 60;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
Zeke Egherman
  • 359
  • 5
  • 17
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – M.kazem Akhgary May 21 '16 at 22:11
  • Just figured out an answer to the question you deleted. Wish you would not have deleted it. – Squashman Jan 24 '17 at 21:11

1 Answers1

5

Use Camera.main instead of Camera.current. Also, The Unity API is not thread safe. You cannot pause the main thread like this. If you want to wait two seconds and then to set all cameras to the same FOV then you could use:

void Start()
{
    //This starts the coroutine.
    StartCoroutine(PauseAndSetFOV());     
}

// This is a coroutine.
private IEnumerator PauseAndSetFOV()
{
    // This waits for a specified amount of seconds
    yield return new WaitForSeconds(2f);

    // This sets all the cameras FOV's after waiting two seconds.
    for(int i = 0; i < Camera.allCamerasCount; i++)
    {
        Camera.allCameras[i].fieldOfView = 60;
    }
}

The function that returns IEnumerator is a coroutine. This is how to do multiple things at the same time in Unity. It is not threading though.

Danny Herbert
  • 2,002
  • 1
  • 18
  • 26
  • that doesnt give me the error, but it doesnt set the FOV – Zeke Egherman May 21 '16 at 22:25
  • If the camera you are trying to set isn't your main camera then this wont work. You will need to get a reference to the camera you want to set and then use that. – Danny Herbert May 21 '16 at 22:28
  • You'll have to google 'Get a reference to a game object Unity' It's been answered very many times before. As has how to solve a Null Reference Exception. – Danny Herbert May 21 '16 at 22:30
  • @ZekeEgherman Check my answer I updated it with a method you could use to set every camera in your scene. You should look into how to do things like get references and debug NulLRefExceptions though. Best of luck man. – Danny Herbert May 21 '16 at 22:41
  • @gjttt1 This is close. You can use threading in unity but there are many rules to follow. In fact,you should use `Thread` when doing simulations such as water. Although using `System.Threading.Thread.Sleep(2000);` is not good in main thread like he did. You can do use that function in another Thread. Why not update your question and replace `System.Threading.Thread.Sleep(2000);` with coroutine and `yield return new WaitForSecond`? That would make this answer complete. – Programmer May 21 '16 at 22:49
  • Shall do. I'll update and say that it's Just the Unity API that isn't thread safe too. (which is correct, right? I've not used threading much). – Danny Herbert May 21 '16 at 22:53
  • 2
    Yes, Unity API isn't thread safe which means that you can't call Unity functions API functions from another Thread. That's not a problem because you can do some heavy calculations in another Thread then notify the main Thread which will then call the Unity API functions from main/Unity's function. Just tell him he doesn't need Thread to do this. Start coroutine, wait for two seconds with `yield return new WaitForSeconds` then run the current code in your answer. Done. No Thread required. – Programmer May 21 '16 at 23:08
  • Awesome, thanks for the advise again man. Useful as ever! Already Updated the answer! – Danny Herbert May 21 '16 at 23:12
  • @gjttt1 No problem. Good answer. +1 – Programmer May 21 '16 at 23:22
  • @ZekeEgherman This is what you are looking for. You should accept this answer if your problem is solved. Otherwise you can comment what is not working. – Programmer May 21 '16 at 23:22