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

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:

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

public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {

    public Transform parentToReturnTo = null;
    public Transform placeholderParent = null;

    public enum Slot {Monster, Item, Spell, Impact, Set};
    public Slot CardType = Slot.Monster;

    GameObject placeholder = null;

    public void OnBeginDrag(PointerEventData eventData) {
        Debug.Log ("OnBeginDrag");

        placeholder = new GameObject ();
        placeholder.transform.SetParent (this.transform.parent);

        LayoutElement le = placeholder.AddComponent<LayoutElement> ();

        le.preferredWidth = this.GetComponent<LayoutElement> ().preferredWidth;
        le.preferredHeight = this.GetComponent<LayoutElement> ().preferredHeight;
        le.flexibleWidth = 0;
        le.flexibleHeight = 0;

        placeholder.transform.SetSiblingIndex (this.transform.GetSiblingIndex());

        parentToReturnTo = this.transform.parent;
        placeholderParent = parentToReturnTo;

        this.transform.SetParent (this.transform.parent.parent);

        GetComponent<CanvasGroup> ().blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData) {
        //Debug.Log ("OnDrag");

        this.transform.position = eventData.position;

        if (placeholder.transform.parent != placeholderParent)
            placeholder.transform.SetParent (placeholderParent);

        int newSiblingIndex = placeholderParent.childCount;

        for(int i=0; i < placeholderParent.childCount; i++){
            if (this.transform.position.x < placeholderParent.GetChild (i).position.x){

                newSiblingIndex = i;

                if (placeholder.transform.GetSiblingIndex () < newSiblingIndex)
                    newSiblingIndex--;

                break;
            }
        }

        placeholder.transform.SetSiblingIndex (newSiblingIndex);
    }

    public void OnEndDrag(PointerEventData eventData) {
        Debug.Log ("OnEndDrag");

        this.transform.SetParent (parentToReturnTo);
        this.transform.SetSiblingIndex (placeholder.transform.GetSiblingIndex());

        GetComponent<CanvasGroup> ().blocksRaycasts = true;
        Destroy (placeholder);
    }
}

Field:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Field : MonoBehaviour, IPointerEnterHandler, IDropHandler, IPointerExitHandler{

    public Drag.Slot CardType = Drag.Slot.Monster;

    public void OnPointerEnter(PointerEventData eventData) {
        //Debug.Log ("OnPointerEnter");
        if (eventData.pointerDrag == null)
            return;

        Drag d = eventData.pointerDrag.GetComponent<Drag> ();

        if (d != null) {
            d.placeholderParent = this.transform;
        }
    }

    public void OnPointerExit(PointerEventData eventData) {
        //Debug.Log ("OnPointerExit");
        if (eventData.pointerDrag == null)
            return;
     
        Drag d = eventData.pointerDrag.GetComponent<Drag> ();

        if (d != null && d.placeholderParent == this.transform) {
            d.placeholderParent = d.parentToReturnTo;
        }
    }

    public void OnDrop(PointerEventData eventData) {
     

        Drag d = eventData.pointerDrag.GetComponent<Drag> ();

        if (d != null) {
            if (CardType == d.CardType) {
                d.parentToReturnTo = this.transform;
                Debug.Log (eventData.pointerDrag.name + "was dropped on the " + gameObject.name + " as a " + CardType);
            } else {
                Debug.Log (eventData.pointerDrag.name + "can't be dropped on the " + gameObject.name + " as a " + CardType);
                return;
            }
        }
    }
}

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
1 Like

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 :slight_smile:

You can use the OnValidate method to do things when the value is updated through the inspector.

3 Likes

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