-1

I have a script in which exists a fade prefab!

The following code snippet instantiates the prefab during run-time, as can be seen in the attached image.

public void Begin( float fadingTime ) { this.fadingTime = fadingTime;

fade = Instantiate(fadePrefab) as GameObject;
fade.transform.parent = Camera.main.transform;
fade.transform.localPosition = new Vector3( 0, 0, 0.31f );
fade.transform.localRotation = Quaternion.identity;

I still get this error message though:

NullReferenceException: Object reference not set to an instance of an object SceneManager.Begin (Single fadingTime) (at Assets/Scripts/SceneManager.cs:57)

Given that I can see the prefab is cloned, I am confused why I get this error message. Please see the attached pic.

So, I thought perhaps I should include something in the code to see if the prefab is indeed instantiated, but I don´t know how to test this.

For example, sometimes we put in such a thing to see if something has some value:

Debug.Log( "I get to this point" + someVariableValue );

What do I test for or how do I do this test, because I have no clue why I am gettig this err msg...

Thank you,

enter image description here

  • 3
    [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) – Soner Gönül Feb 03 '15 at 13:27
  • I did include Debug.Log( "NULL!" + ( fade == null ) ); but nothing is displayed at the console. Is this how you would test it? –  Feb 03 '15 at 13:33
  • Do you want to instantiate Oculus Prefab? – Barış Çırıka Feb 03 '15 at 16:02

1 Answers1

0

Probably Camera.main is null if you don't have a Camera enabled in your scene.

Add a camera named MainCamera to your scene to fix the problem.

Jordi Cruzado
  • 770
  • 5
  • 12
  • Thanks JordiC. I think you are right, because the following line of code should return 56 which is the 'x' coordinate of my main camera (what I get from a "working" version of my application on a different machine) but here I get nothing: Debug.Log( "Camera.main.transform.x" + Camera.main.transform.position.x ); –  Feb 03 '15 at 17:40
  • But since I have a couple of cameras, as well as a couple that come with OVRCameraRig and OVRPlayerController, I have no clue how to determine which one... Actually, I have checked them all, and it all seems to be in order. My application (originally developed by someone else) loads the cams dynamically, so I am wondering how to "find out" which one is the main cam that is not returning anything... –  Feb 03 '15 at 17:41
  • 1
    In fact you are using the Camera transform just as parent of your GameObject (fade). You can use another GameObject as parent or even let fade.transform.parent=null;. If you prefer that the parent of fade is a camera then use the transform of OVRCameraRig or OVRPlayerController Cameras as parents of fade. – Jordi Cruzado Feb 04 '15 at 09:14