Programming Error

Hi my name is Jose, im From Argentina Sorry about my English :p. Im new in this world, i started with the book Learning C# by developing games with Unity 3D, and im stack here:

using UnityEngine;
using Assets.Interfaces;
using System.Collections;

namespace Assets.States
{
    public class SetupState : IStateBase
    {
        private StateManager manager;
        private GameObject player;
        private PlayerControl controller;

        public SetupState (StateManager managerRef)
        {
            manager = managerRef;
            player = GameObject.Find("Jugador");
            //Debug.Log (player);
            controller = player.GetComponent<PlayerControl>();
            if (Application.loadedLevelName != "Scene1")
            {
                Application.LoadLevel ("Scene1");
            }
        }
        public void StateUpdate()
        {
            if(Input.GetKeyUp(KeyCode.DownArrow))
              {
                Debug.Log ("Estamos dentro del If de rotacion");
              controller.transform.Rotate(0, controller.setupSpinSpeed * Time.deltaTime, 0);
            }
        }
        public void ShowIt()
              {

            }
    }
}

and the error:
NullReferenceException: Object reference not set to an instance of an object
Assets.States.SetupState…ctor (.StateManager managerRef) (at Assets/States/SetupState.cs:18)
Assets.States.BeginState.Switch () (at Assets/States/BeginState.cs:42)
Assets.States.BeginState.ShowIt () (at Assets/States/BeginState.cs:31)
StateManager.OnGUI () (at Assets/Scripts/StateManager.cs:45)

testing some thing i discover that player variable is null, but a i cant understand why, someone can help me please, Thanx.

So you are right, it seems player variable is null. Two lines above you see player is the result of GameObject.Find() call.

Check that in Scene there is object called Jugador and it is active, because GameObject.Find() returns only active gameobjects[/code]

Yeah this is my scene1: the Object Jugador is a cube and i think is active too.

when i print player variable show me null, and when i add

controller = player.GetComponent();

this line make the error, i think is because the variable is null cant get the component.

Look, you try to get the player before you load the scene.

I test that but doesnt work, the Scene is previous load on BeginState:

using UnityEngine;
using Assets.Interfaces;
namespace Assets.States
{
    public class BeginState : IStateBase
    {
        private StateManager manager;

        public BeginState (StateManager managerRef)
        {
            //Debug.Log("Dentro del Constructor de BeginState");
            manager = managerRef;
            if(Application.loadedLevelName != "PantallaInicio")
                Application.LoadLevel("PantallaInicio");
            //Time.timeScale = 0;
        }
        public void StateUpdate()
        {
            if (Input.GetKeyUp (KeyCode.Space))
            {
                //Debug.Log("Dentro del If de StateUpdate");
                //manager.SwitchState (new PlayState (manager));
            }
        }
        public void ShowIt()
        {
            GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), manager.gameDataRef.beginStateSplash, ScaleMode.StretchToFill);
            GUI.Box(new Rect(10,10,100,90), "Menu");
            if (GUI.Button(new Rect(20, 40, 80, 20), "Jugar"))
            {
                Switch ();
            }
            if (GUI.Button(new Rect(20, 70, 80, 20), "Opciones"))
            {
                Switch ();
            }
        }
        public void Switch()
        {
            //Time.timeScale = 1;
   Application.LoadLevel("Scene1"); // here i load the scene
            manager.SwitchState (new SetupState (manager));
        }
    }
}

Ok i found something… if i put

player = GameObject.Find(“Jugador”);
//Debug.Log (player);
controller = player.GetComponent();

inside StateUpdate method it takes two or three time to get the object Jugador:

public void StateUpdate()
{
if (player != null)
{
player = GameObject.Find("Jugador");
//Debug.Log (player);
controller = player.GetComponent<PlayerControl>();
}
}
}

inside the Update it takes two or three times to found the player, it works but the question is why doesnt work in the construct. maybe have to make a while to wait to object to be found?

I continue working, if someone know the answer please help me :p.

Please, use CODE tags when posting source codes. Makes it easier to read.