Scripts not being applied to game object

I have a game object with a script attached to it. Sometimes when I update the script the changes don’t seem to get applied to the script attached to the game object even though there don’t seem to be any errors in the code.

For example, if I add something like

public int testInt = 1;

when I refresh Unity and select the game object with the script attached the int won’t always show up in the inspector. If I select the script itself I can see the correct code in the inspector.

Deleting and reattaching the script doesn’t help. Repeatedly changing the script and saving sometimes does Restarting Unity sometimes does.

I’ve tried googling the problem, but I can’t seem to find anyone with the exact same issue. (I apologize if ten people immediately post links to this exact question) This is extremely frustrating as continually restarting and praying is not a fun way to try to program so any ideas on what’s going wrong would be great.

Here’s the code. Please keep in mind that this is my first attempt at writing anything in Unity so it is both messy and far too long. I think.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class ControlPlayer : MonoBehaviour {



    public int currentDialogueLine = 0; // which dialogue line are we on currently?
    private int[] numberOfOptionsArray; // the number of response options for this current line of dialogue 
    private string[,] dialogue; // NPC text along (first column) with the possible responses (second column)
    private int[,] dialoguePointers; // how to navigate the conversation

    private PointerScript pointerScript;


    public bool canMove = true; // should we try to move?
    public float stepDistance = .04f; // how fast to walk

    private float unitDistance = 1f; // How far should we travel each step
    private float threshold = .25f; // Not really needed. Get rid of this at some point
    private float checkDistance = .6f; // how far ahead should we look for collisions? Also probably not needed

    private Dictionary<int, Vector2> compass = new Dictionary<int, Vector2>();
    private Animator playerAnimator;
    private RaycastHit2D[] objectAhead = new RaycastHit2D[1]; // store the raycast for the thing ahead of us
    private RaycastHit2D blankRaycast;
    public GenericNPC NPCScript; // the NPC script of the thing ahead of us
    private GameObject NPC = null; // the NPC ahead of us
    public Vector2 currentDirection; //the direction AS OF THE LAST TIME WE PRESSED THE INTERACT BUTTON!!!

    public GameObject NPCPanel;
    public Text NPCText;
    public GameObject playerPanel;
    public Text playerText;
    public Image pointer;

    // Use this for initialization
    void Start ()
    {
        playerAnimator = GetComponent<Animator>(); // 1 is north, 2 is east, 3 is south, 4 is west
        compass.Add(1,Vector2.up);
        compass.Add(2,Vector2.right);
        compass.Add(3,Vector2.down);
        compass.Add(4, Vector2.left);

        pointerScript = pointer.GetComponent<PointerScript>();


        numberOfOptionsArray = new int[10] { 3, 1, 2, 2, 1, 1, 1, 1, 1, 1 };

        pointerScript.numberOfOptions = numberOfOptionsArray[currentDialogueLine];

        dialogue = new string[10, 2] {
            { "Good morning miss Finagreek! How are you on this fine day?", "Great!

Terrible!
Miss Finagreek? I’m a pirate!" }, //0
{ “That’s good to hear”, “Bye.” }, //1
{ “I’m so sorry to hear that. Is there anything I can do to make your stay here more comfortable?”, “Let me go!
Have you heard of Selkies?” }, //2
{ “Oh, yes, of course. I do apologize miss pirate Finugreek”, “…
Flip him off!” }, //3
{ “You know I can’t do that, but while you’re here you can be sure we will stop at nothing to make sure you’re comfortable… except of course we can’t give you any musical instruments or lift this anti-magic zone.”, “Bye, loser.” }, //4
{ “Selkies? Never heard of them!”, “Bye.” }, //5
{ “Oh my! That’s no way to speak to your doctor!”, “Bye” }, //6
{ “0”, “0” },
{ “0”, “0” },
{ “0”, “0” }
};

        dialoguePointers = new int[10, 5];  // {dialogue line, option} value is new line
        dialoguePointers[0, 0] = 1;
        dialoguePointers[0, 1] = 2;
        dialoguePointers[0, 2] = 3;
        dialoguePointers[2, 0] = 4;
        dialoguePointers[2, 1] = 5;
        dialoguePointers[1,0]=100;
        dialoguePointers[3,0]=100;
        dialoguePointers[3,1]=6;
        dialoguePointers[4,0]=100;
        dialoguePointers[5,0]=100;
        dialoguePointers[6,0]=100;

        blankRaycast = new RaycastHit2D();
        objectAhead[0] = blankRaycast;
    }
	
	//----------------------------------------------------Update is called once per frame------------------------------------------------//
	void Update ()
    {
        Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y); // get the player's position as a 2d vector. Always do this.



         //------------------------------------------------------------Interact with thing ahead-------------------------------------------------------//
        if (Input.GetButtonDown("Interact") && !NPCPanel.activeInHierarchy){ // check to see if you should start interacting

            currentDirection = compass[playerAnimator.GetInteger("Direction")]; //which direction are we facing?
            Physics2D.LinecastNonAlloc(playerPosition + (currentDirection * .55f), playerPosition + (currentDirection * checkDistance), objectAhead); // what's ahead of us?

            if (objectAhead[0].transform != null) { //did we actually find an NPC?
                NPC = objectAhead[0].transform.gameObject;
                if (NPC.GetComponent<GenericNPC>() != null) { // if they're an npc start talking
                    {
                        NPCScript = NPC.GetComponent<GenericNPC>();
                        canMove = false;
                        NPCPanel.SetActive(true);
                        playerPanel.SetActive(true);
                        currentDialogueLine = 0;
                        NPCText.text = dialogue[currentDialogueLine, 0];
                        playerText.text = dialogue[currentDialogueLine,1];
                        pointerScript.numberOfOptions = numberOfOptionsArray[currentDialogueLine];

                    }
                }
             }
        }


        //------------------------------------------------------Options while talking-----------------------------------------------------//
        else if (NPCPanel.activeInHierarchy)// Are we currently talking to anyone?
        {
            if (Input.GetKeyDown("escape")) { // should we stop talking?
                StopTalking();
            }

            if (Input.GetButtonDown("Interact") && playerPanel.activeInHierarchy) // should we advance the dialogue?
            {
                currentDialogueLine = dialoguePointers[currentDialogueLine, -pointerScript.pointerPosition]; // where should the conversation go next?
                if (currentDialogueLine >= dialogue.Length) // if it's too high we're done talking
                {
                    StopTalking();
                }
                else // otherwise move on to the next conversational stage
                {
                    AdvanceDialogue();
                }
            }             
            
        }








        //----------------------------------------------------------------Movement control-------------------------------------------------------------//
        if (canMove) //Stuff below here is movement
        {

            if (Input.GetAxis("Horizontal") > threshold) // east
            {
                canMove = false;
                playerAnimator.SetInteger("Direction", 2);
                playerAnimator.SetBool("Moving", true);
                if (Physics2D.LinecastNonAlloc(playerPosition + (Vector2.right * .55f), playerPosition + (Vector2.right * checkDistance), objectAhead) == 0) 
                {
                    Vector2 target = playerPosition + Vector2.right * unitDistance;
                    StartCoroutine(Move(target, playerPosition));
                }
                else
                {
                    canMove = true;
                    playerAnimator.SetBool("Moving", false);
                }
            }
            else if (Input.GetAxis("Horizontal") < -threshold) //west
            {
                canMove = false;
                playerAnimator.SetInteger("Direction", 4);
                playerAnimator.SetBool("Moving", true);
                if (Physics2D.LinecastNonAlloc(playerPosition + (Vector2.left * .55f), playerPosition + (Vector2.left * checkDistance), objectAhead) == 0)
                {
                    Vector2 target = playerPosition + Vector2.left * unitDistance;
                    StartCoroutine(Move(target, playerPosition));
                }
                else
                {
                    canMove = true;
                    playerAnimator.SetBool("Moving", false);
                }
            }
            else if (Input.GetAxis("Vertical") > threshold) //north
            {
                canMove = false;
                playerAnimator.SetInteger("Direction", 1);
                playerAnimator.SetBool("Moving", true);
                if (Physics2D.LinecastNonAlloc(playerPosition + (Vector2.up * .55f), playerPosition + (Vector2.up * checkDistance), objectAhead) == 0)
                {
                    Vector2 target = playerPosition + Vector2.up * unitDistance;
                    StartCoroutine(Move(target, playerPosition));
                }
                else
                {
                    canMove = true;
                    playerAnimator.SetBool("Moving", false);
                }
            }
            else if (Input.GetAxis("Vertical") < -threshold) //south
            {
                canMove = false;
                playerAnimator.SetInteger("Direction", 3);
                playerAnimator.SetBool("Moving", true);
                if (Physics2D.LinecastNonAlloc(playerPosition + (Vector2.down * .55f), playerPosition + (Vector2.down * checkDistance), objectAhead) == 0)
                {
                    Vector2 target = playerPosition + Vector2.down * unitDistance;
                    StartCoroutine(Move(target, playerPosition));
                }
                else
                {
                    canMove = true;
                    playerAnimator.SetBool("Moving", false);
                }
            }
            else
            {
                playerAnimator.SetBool("Moving", false);
            }
         }
	}

    public void StopTalking()
    {
        canMove = true;
        NPCPanel.SetActive(false);
        playerPanel.SetActive(false);
        objectAhead[0] = blankRaycast;
        pointerScript.pointerPosition = 0;
    }

    public void AdvanceDialogue()
    {
        pointerScript.numberOfOptions = numberOfOptionsArray[currentDialogueLine];
        NPCText.text = dialogue[currentDialogueLine, 0];
        playerText.text = dialogue[currentDialogueLine, 1];
        pointerScript.pointerPosition = 0;
    }
        
    IEnumerator Move(Vector2 target, Vector2 playerPosition)
    {
        float distance = stepDistance;

        while (distance < (unitDistance + stepDistance))
        {
            Vector2 temp = Vector2.Lerp(playerPosition, target, distance);
            transform.position = new Vector3(temp.x, temp.y, 0);
            distance += stepDistance;
            yield return null;
        }
        canMove = true;
        yield return null;
    }

}

Usually it takes some time for the script to be serialized.
What you experienced can be due to:

  1. Script not saved.
  2. Script saved with errors.
  3. variable hidden in inspector.

If the inspector doesn’t seem to be up to date, you can select another gameObject then come back to the one you want to check.