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

Please Help, I do not understand why this error occurs. Im new to unity but I already knew a bunch about javascript but this is making me insane, I cant find why.

Code for GameManager

var currentAmount:int;
var currentColor:int;
var currentMax:int;
var thisRoundsColors = [];
var TouchInput : TouchInputJavascript;
var MC : MainCube;

function Start()
{
    MC = gameObject.GetComponent("MainCube");
    TouchInput = gameObject.GetComponent("TouchInputJavascript");
    yield WaitForSeconds(3);
    Debug.Log("Starting");
    nextRound();
}

function Red() {
    if (thisRoundsColors[currentColor] == 3)
    {
        currentColor += 1;
        if(currentColor >= currentMax)
        {
            nextRound();
        }
        else{
            Application.LoadLevel(0);
        }
    }
}

function Blue() {
    if (thisRoundsColors[currentColor] == 2)
    {
        currentColor += 1;
        if(currentColor >= currentMax)
        {
            nextRound();
        }
        else{
            Application.LoadLevel(0);
        }
    }
}

function Green() {
    if (thisRoundsColors[currentColor] == 1)
    {
        currentColor += 1;
        if(currentColor >= currentMax)
        {
            nextRound();
        }
        else{
            Application.LoadLevel(0);
        }
    }
}

function Yellow() {
    if (thisRoundsColors[currentColor] == 4)
    {
        currentColor += 1;
        if(currentColor >= currentMax)
        {
            nextRound();
        }
        else{
            Application.LoadLevel(0);
        }
    }
}

function newColors(newColors:Array){
    for(var i:int = 0; i<newColors.length; i++ )
    {
        thisRoundsColors[i] = newColors[i];
    }
    currentMax = thisRoundsColors.length;
    TouchInput.canClick = 0;
}

  function nextRound() {
    Debug.Log("New Round");
    currentAmount += 1;
    currentColor = 1;
    Debug.Log("This is now round: "+currentAmount);

    Debug.Log("Sending request for new colors...");
    MC.displayColor(currentAmount);
}

Code for MainCube

var greenColor : Color;
var blueColor : Color;
var redColor : Color;
var yellowColor : Color;
var DefaultColor : Color;
var mat : Material;
var duration : int;
var newColors:Array;
var GM : GameManager;

function Start() {
    GM = gameObject.GetComponent("GameManager");
}

function Test() {
    Debug.Log(" It Works!");
}

function displayColor(amount:int)
{
    Debug.Log("Starting to show colors");
    for(var i:int = 0; i < amount; i++)
    {
        var NowColor = ReturnRandomNumber();
        if (NowColor == 1)
        {
            displayGreen();
            newColors[i] = NowColor;
            yield WaitForSeconds(duration);
        }
        else if (NowColor == 2)
        {
            displayBlue();
            newColors[i] = NowColor;
            yield WaitForSeconds(duration);
        }
        else if (NowColor == 3)
        {
            displayRed();
            newColors[i] = NowColor;
            yield WaitForSeconds(duration);
        }
        else if (NowColor == 4)
        {
            displayYellow();
            newColors[i] = NowColor;
            yield WaitForSeconds(duration);
        }
    }
    mat.color = DefaultColor;
    GM.newColors(newColors);
}

  function ReturnRandomNumber()
{
    var randomNumber : int = Random.Range(1,4);
    return randomNumber;
}

  function displayGreen() {
    mat.color = greenColor;
}

  function displayBlue(){
    mat.color = blueColor;
}

  function displayRed(){
    mat.color = redColor;
}

  function displayYellow(){
    mat.color = yellowColor;
}

Code for TouchInputJavascript

var touchInputMask : LayerMask;
var GameHasStarted : int;
var canClick : int;

function Start () {
    GameHasStarted = 0;
    yield WaitForSeconds(3);
    GameHasStarted = 1;
}

function Update ()
{
if(GameHasStarted == 1 && canClick == 1)
{
    if (Input.touchCount > 0)
    {
 
        for (var touch:Touch in Input.touches)
        {
            var ray : Ray = camera.ScreenPointToRay(touch.position);
            var hit : RaycastHit;
            if (Physics.Raycast(ray,hit,100,touchInputMask))
            { 
                Debug.Log("Hit something");
                var recipient : GameObject = hit.transform.gameObject;
         
                if (touch.phase == TouchPhase.Began)
                {
                    recipient.SendMessage ("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Ended)
                {
                    recipient.SendMessage ("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Stationary)
                {
                    recipient.SendMessage ("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Moved)
                {
                    recipient.SendMessage ("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
}
}

Code for Button

var defaultColour : Color;
var selectedColour : Color;
var mat : Material;
var thisCube : String;
var GM : GameManager;

function Start () {
    GM = gameObject.GetComponent("GameManager");
    mat = renderer.material;
    mat.color = selectedColour;
    yield WaitForSeconds(3);
    mat.color = defaultColour;
}

function OnTouchDown() {
    mat.color = selectedColour;
    sendInfo();
}
function OnTouchUp() {
    mat.color = defaultColour;
    sendInfo();
}
function OnTouchStay() {
    mat.color = selectedColour;
    sendInfo();
}
function OnTouchExit() {
    mat.color = defaultColour;
    sendInfo();
}

function sendInfo(){
if (thisCube == "green"){
    GM.Green();
}
if (thisCube == "blue"){
    GM.Blue();
}
if (thisCube == "red"){
    GM.Red();
}
if (thisCube == "yellow"){
    GM.Yellow();
}
}

Full Error:
NullReferenceException: Object reference not set to an instance of an object
GameManager.nextRound () (at Assets/GameManager.js:90)
GameManager+$Start$10+$.MoveNext () (at Assets/GameManager.js:15)

Warning:
The referenced script on this Behaviour is missing!

Made For:
Android Xperia from Sony and with Unity remote 4

Game should end up as a Simon Says game. And by the way, I’ve looked in to the API for gameObject.GetComponent and dubble checked capital letters, this should’nt give an error :frowning: but it does.
All love and respect to whom helps me, kind regards -Erik

Oh and by the way, in the Inspector when I click on GameManager / Button / MainCube under its script where it should show variables and such, all the TouchInput / GM / MainCube says : GM [none (Game Manager) ] or MainCube [none ( Main Cube)] or TouchInput [none (TouchInputJavascript)]

The problem is that MC is null in the NextRound method because of the error you posted in the second comment. The scripts are no longer connected to the component that is attached to the game object.

I can’t say why it happened, but I’ve seen it before when a script is renamed or moved in the file system outside of Unity.

I’d suggest removing the scripts from the game object and re-adding them.

Thank you so much for taking time and helping, but I already spolved the problem and the app is soon on amazon appstore :stuck_out_tongue: