NullReferenceException: Object reference not set to an instance of an object

I could really use some help here. I have been fighting this for three days now. I am making a 2d side scroller, and am working on a menu to allow the player to choose a character to play as. I have created three scripts: PlayerController, PlayerStates, and MainMenu.

I am using a texture for buttons in the OnGUI method so the player can choose the character they want to play as. Each button should change the enum variable choice to character1 or character2. I have a method characterSelect in PlayerControls to decide which animation to play based on what choice is set to. My problem is, when I choose a character I get the NullReferenceException. Below is my code for my scripts. I have noted in the PlayerControll script where the Error is at. It is in my characterSelect method. Ant help would be greatly appreciated. It is probably something simple. :slight_smile:

MainMenu.cs

using UnityEngine;
using System.Collections;

public class MainMenu : MonoBehaviour
{
    
    public Texture backgroundTexture;

    public character choice;
    public Texture2D Player;
    public Texture2D Player1;

    public float guiPlacementY1;
    public float guiPlacementY2;

    public float guiPlacementX1;
    public float guiPlacementX2;

    


    void OnGUI()
    {
        

        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backgroundTexture);

        if (GUI.Button(new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), Player, ""))
        {

            
            choice = character.character1;
            
            Application.LoadLevel(1);
        }


        if (GUI.Button(new Rect(Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .5f, Screen.height * .1f), Player1, ""))
        {            

            choice = character.character2;       

            Application.LoadLevel(1);
        }
    }

	
}

PlayerController.cs

using UnityEngine;
using System.Collections;



public class PlayerController : MonoBehaviour
{    
    public float maxSpeed = 10f;
    bool facingRight = true;

    Animator anim;

    bool grounded = true;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;

	// Use this for initialization
	void Start () 
    {
        
        anim = GetComponent<Animator>();
        characterSelect();
        
	
	}
	
	// Update is called once per frame
	void FixedUpdate () 
    {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool("Ground", grounded);

        anim.SetFloat("vSpeed", rigidbody2D.velocity.y);



        float move = Input.GetAxis("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(move));

        rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);

        if (move > 0 && !facingRight)
            Flip();
        else if (move < 0 && facingRight)
            Flip();
	
	}

    void Update()
    {
        if (grounded && Input.GetKeyDown(KeyCode.Space))
        {
            anim.SetBool("Ground", false);
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;

    }

    void characterSelect()
    {
        
        anim.GetComponent<PlayerController>();


        if (GetComponent<MainMenu>().choice == character.character1) //NullExceptionError here
        {
            anim.Play("Idle");
        }
        else if (GetComponent<MainMenu>().choice == character.character2)
        {
            anim.Play("Idle1");
        }
    }

    
}

PlayerStates.cs

using UnityEngine;
using System.Collections;


public enum character
{
    character1,
    character2
}




public class PlayerStates : MonoBehaviour 
{
   


}

The line that is causing the exception is in the characterSelect() function, which is called from Start() in the PlayerController.

However, the variable that it is trying to access, choice, is only conditionally assigned in the OnGUI method of the MainMenu (e.g. choice = character.character1;), when the player clicks on one of the two buttons. So, at the point that PlayerController is trying to access it (in the Start() method), it is null, which is what the error message states.