• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gamelan968 · Sep 01, 2017 at 08:23 AM · script loading

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!\nTerrible!\nMiss 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!\nHave you heard of Selkies?" }, //2
             { "Oh, yes, of course. I do apologize miss pirate Finugreek", "...\nFlip 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;
     }
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by adrienPlayerium · Aug 22, 2017 at 03:09 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gamelan968 · Aug 22, 2017 at 03:17 AM 0
Share

Thanks for the response!

How long does it take Unity to serialize scripts? Whenever I switch back to Unity after saving a script it hangs for a moment. I assumed that's what it was doing, but maybe it takes longer than that? Also, how would I check for each of these possibilities?

  1. I've been very careful to save the script before switching back Unity. Is that what you mean?

  2. Unity isn't picking up any errors and I can't find any indication of an error from $$anonymous$$VS. Is there a way to check more thoroughly?

  3. I don't think this is the case. It looks like you have to go out of your way to hide public variables in the inspector, and I have never done this.

I've tried selecting other game objects, closing Unity, reattaching the script, and none of these things consistently work.

avatar image adrienPlayerium gamelan968 · Aug 22, 2017 at 03:20 AM 0
Share

Actually it should be fast to serialize~ What version of unity are you using? $$anonymous$$aybe update to the latest ones. It is hard to guess whats happening, have you tried from another computer? unity version? $$anonymous$$aybe share the faulty script I can try to have a look ~

avatar image gamelan968 gamelan968 · Aug 22, 2017 at 04:01 AM 0
Share

I added the code to my question. I'm using Unity 2017.1 which I think is the latest version. I haven't tried from another computer, I'll do that tomorrow.

I added the script to my question, but as I said it might be too long to easily look at.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't add script; Script has not finished compilation yet. 0 Answers

script isn't loading (beginner question) 1 Answer

Loading a font via WWW 0 Answers

Why is my MonoBehaviour never being deleted? 0 Answers

How to package script as file and load it like AssetBundle? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges