How to create a texture from an image and assign it to a skykbox

This is what I found but no answer.

I used exactly the same method.

		skyBox = mainCamera.gameObject.AddComponent<Skybox>();
		Texture texture = Resources.Load("mario") as Texture; 
		if(texture != null){
		 	Debug.Log(texture.width);
			Debug.Log(texture.height);
			skyBox.material.mainTexture = texture;
		}

This is my code. The output is 1024(width) and 512(height), so the texture is not a null. But when assigned to skybox, NullReferenceException throws.

Please help. Thanks.

This line is meaningless to Unity:

mainCamera.gameObject.AddComponent();

Please see the documentation for AddComponent.

Also, as LightSource said, there are other ways to assign an image if you’re not trying to do it at runtime. What exactly are you trying to do?