Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing a variable's value in the Inspector doesn't update the Script

Discussion in 'Scripting' started by Nervly, Jun 27, 2017.

  1. Nervly

    Nervly

    Joined:
    Mar 15, 2016
    Posts:
    19
    Greetings!

    I'm a beginner to both Unity and programming. While following a tutorial, I decided to add an extra little touch of my own using the things I had learned so far, however, I was faced with some problems.

    Basically, I had two scripts:

    Drag:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
    8.  
    9.     public Transform parentToReturnTo = null;
    10.     public Transform placeholderParent = null;
    11.  
    12.     public enum Slot {Monster, Item, Spell, Impact, Set};
    13.     public Slot CardType = Slot.Monster;
    14.  
    15.     GameObject placeholder = null;
    16.  
    17.     public void OnBeginDrag(PointerEventData eventData) {
    18.         Debug.Log ("OnBeginDrag");
    19.  
    20.         placeholder = new GameObject ();
    21.         placeholder.transform.SetParent (this.transform.parent);
    22.  
    23.         LayoutElement le = placeholder.AddComponent<LayoutElement> ();
    24.  
    25.         le.preferredWidth = this.GetComponent<LayoutElement> ().preferredWidth;
    26.         le.preferredHeight = this.GetComponent<LayoutElement> ().preferredHeight;
    27.         le.flexibleWidth = 0;
    28.         le.flexibleHeight = 0;
    29.  
    30.         placeholder.transform.SetSiblingIndex (this.transform.GetSiblingIndex());
    31.  
    32.         parentToReturnTo = this.transform.parent;
    33.         placeholderParent = parentToReturnTo;
    34.  
    35.         this.transform.SetParent (this.transform.parent.parent);
    36.  
    37.         GetComponent<CanvasGroup> ().blocksRaycasts = false;
    38.     }
    39.  
    40.     public void OnDrag(PointerEventData eventData) {
    41.         //Debug.Log ("OnDrag");
    42.  
    43.         this.transform.position = eventData.position;
    44.  
    45.         if (placeholder.transform.parent != placeholderParent)
    46.             placeholder.transform.SetParent (placeholderParent);
    47.  
    48.         int newSiblingIndex = placeholderParent.childCount;
    49.  
    50.         for(int i=0; i < placeholderParent.childCount; i++){
    51.             if (this.transform.position.x < placeholderParent.GetChild (i).position.x){
    52.  
    53.                 newSiblingIndex = i;
    54.  
    55.                 if (placeholder.transform.GetSiblingIndex () < newSiblingIndex)
    56.                     newSiblingIndex--;
    57.  
    58.                 break;
    59.             }
    60.         }
    61.  
    62.         placeholder.transform.SetSiblingIndex (newSiblingIndex);
    63.     }
    64.  
    65.     public void OnEndDrag(PointerEventData eventData) {
    66.         Debug.Log ("OnEndDrag");
    67.  
    68.         this.transform.SetParent (parentToReturnTo);
    69.         this.transform.SetSiblingIndex (placeholder.transform.GetSiblingIndex());
    70.  
    71.         GetComponent<CanvasGroup> ().blocksRaycasts = true;
    72.         Destroy (placeholder);
    73.     }
    74. }
    75.  

    Field:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class Field : MonoBehaviour, IPointerEnterHandler, IDropHandler, IPointerExitHandler{
    7.  
    8.     public Drag.Slot CardType = Drag.Slot.Monster;
    9.  
    10.     public void OnPointerEnter(PointerEventData eventData) {
    11.         //Debug.Log ("OnPointerEnter");
    12.         if (eventData.pointerDrag == null)
    13.             return;
    14.  
    15.         Drag d = eventData.pointerDrag.GetComponent<Drag> ();
    16.  
    17.         if (d != null) {
    18.             d.placeholderParent = this.transform;
    19.         }
    20.     }
    21.  
    22.     public void OnPointerExit(PointerEventData eventData) {
    23.         //Debug.Log ("OnPointerExit");
    24.         if (eventData.pointerDrag == null)
    25.             return;
    26.      
    27.         Drag d = eventData.pointerDrag.GetComponent<Drag> ();
    28.  
    29.         if (d != null && d.placeholderParent == this.transform) {
    30.             d.placeholderParent = d.parentToReturnTo;
    31.         }
    32.     }
    33.  
    34.     public void OnDrop(PointerEventData eventData) {
    35.      
    36.  
    37.         Drag d = eventData.pointerDrag.GetComponent<Drag> ();
    38.  
    39.         if (d != null) {
    40.             if (CardType == d.CardType) {
    41.                 d.parentToReturnTo = this.transform;
    42.                 Debug.Log (eventData.pointerDrag.name + "was dropped on the " + gameObject.name + " as a " + CardType);
    43.             } else {
    44.                 Debug.Log (eventData.pointerDrag.name + "can't be dropped on the " + gameObject.name + " as a " + CardType);
    45.                 return;
    46.             }
    47.         }
    48.     }
    49. }
    50.  

    So the relevant lines for my question are:
    - 12 & 13 for Drag.cs
    - 8 & 39 to 45 for Field.cs


    With that said, here's my problem:
    - The variable 'CardType' shows up in the Inspector and allows me to switch through Monster, Spell, Item, etc. So far so good. That same variable is set to 'Monster' as default. What's supposed to happen is that when I drag a card to a certain spot, I will get a debug message saying "Card was dropped on the center as a Monster". So far so good. However, here's where the problems surfaces. In the inspector menu, when I switch the CardType to Spell, Item, etc., the debug message will always say "as a Monster" anyway instead of "as a Spell" or "as a Set", etc., even though it recognizes that the card's type isn't Monster because it doesn't let me place the card on the slot (which is supposed to happen)

    So, technically, when I update the card's type in the Inspector, it's as if in the script it never changed. I'm guessing the solution would involve writing a line of code that updated the CardType variable when I change it in the Inspector? I would like to know what's causing this and how can I solve it.

    Thank you in advance,
    - Nervly
     
    Last edited: Jun 27, 2017
    ProGameDevUser likes this.
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Yes, the variables hardcoded into scripts will not change in the script file when editing inspector values. Get used to it, gets me all the time but you'll start realising that it's happening and it'll be all good.

    I don't think you'd want this to happen really, because often the default values in a script might be slightly adjusted in certain situations, so learn to live with it :)
     
  3. Deleted User

    Deleted User

    Guest

    You can use the OnValidate method to do things when the value is updated through the inspector.
     
    ProGameDevUser, MD_Reptile and Kiwasi like this.
  4. Nervly

    Nervly

    Joined:
    Mar 15, 2016
    Posts:
    19
    Thank you for both your replies!

    I just managed to get it working and it seems like all I had to do was change 'Cardtype' to 'd.CardType' in the 44th line.
    Now whenever I change the value in the Inspector, it returns the correct message. Can't believe I didn't think of trying this yesterday ahah