C# : Property being reseted when used in another script

Hi everyone !
I’m working on my first 2D micro-project to learn Unity and C#, and I’m facing a problem I do not understand :
a Property {get;} that I used to get an int works fine in its own script, but is reseted to 0 when called in another one.

In this small projet, a character (“Chocobo”) is on the screen, managed with the script “ChocoboManager.cs”. Its color can change with a script called “PaletteSwapper.cs”.
Both of those scripts are components of the Chocobo GameObject.

Also, several tutorials being used in this micro-project and beacause the one concerning PaletteSwapper is not free*, I only paste here the essential code where the problem appears even in isolated tests)

(oh, and i use the word “Indice” instead of “Index” everytime, my bad :x)

In ChocoboManager.cs :

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

public class ChocoboManager : MonoBehaviour {

    //------------------- VARIABLES USED --------------
  
    // ChocoboColor is a simple enum class in another file : public enum ChocoboColor {Yellow, Green, Blue, Cyan, Purple, Red}
    public ChocoboColor chocoboColor;

    // When paletteIndice is 0, the Chocobo should be Yellow. When 1, it should Green, etc.
    private int paletteIndice;

    //------------------- ---------- --------------

    // Awake method

    public void ColorBaseStats()
    {
        switch(chocoboColor)
        {
        case ChocoboColor.Yellow:
            paletteIndice = 0;
            Debug.Log ("Yellow choosed (#"+paletteIndice+")"); 
            // On the console, shows "Yellow choosed (#0)"
            break;

        case ChocoboColor.Green:
            paletteIndice = 1;
            Debug.Log ("Green choosed (#"+paletteIndice+")"); 
           // On the console, shows "Green choosed (#1)"
            break;  
      
        // Other cases with other colors and paletteIndices, each one having in additions stats modificators (speed, acceleration, etc.) that I do not paste here for clarity.
  
        }

        Debug.Log ("Get = "+ PaletteIndice);          
        // On the console, shows "Get = 0" or "Get = 1" according with the color chosen in the Inspector
    }
      
    public int PaletteIndice { get {return paletteIndice;} }


    public void ChocoboInitialization()
    {
        ColorBaseStats ();
  
    // I call the ColorBaseStats() here instead of Awake() or Start() because I might later move it in a class without MonoBehavior
    // Still, I have tested with Awake or Start and there is no difference, it works as intented
      
    }

So for this file, the property works fine, it takes the value that matches the color being chosen. Problem appears with the second file,
PaletteSwapper.cs :

public class PaletteSwapper : MonoBehaviour {

    private ChocoboManager chocoboManager = null;
    private int paletteIndice;

    void Awake()
    {
        // Retrieve the palette#
        chocoboManager = gameObject.AddComponent(typeof(ChocoboManager)) as ChocoboManager;
        paletteIndice = chocoboManager.PaletteIndice;
    }


    void Start ()
    {
        if(palettes.Length>0)
            SwapColors (palettes[paletteIndice]);

        Debug.Log (paletteIndice);
        // On the console, always shows "0", even when I have "Get = 1" before.
    }

The paletteIndice being always read as 0 by PaletteSwapper.cs, the color never changes.

I can’t show the SwapColors method because it is from a paid tutorial, but it works fine (it changes the original colors to the ones made in a palette, those palettes being in an array).
For exemple, if I use directly :
SwapColors (palettes[1]);
then the character will have the good color.

I have also tested [SerializedField] and putting everything on public, but the problem is still the same.
I’ve made several searches in French or English, have redone several tutorials and checked the use of Properties, but I cannot find why there is connections in others previous works and not here (also, I’ve redone several exercices on WPF or WinForm where this kind of connections works, so I may have taken some bad habits ^^’ ).

I’ts something that I think is pretty basic but I do not have enough knowledge and the good point of view to understand why it doesn’t work.

I’m sorry to bother the users of this forum with such a dumb problem, and thank you in advance for your help ! :smile:

Well, at a glance, you’re getting the paletteIndice in awake, but the value is 0 at that point. Even if you change the value later, awake gets it once at the start and it’s 0 at that point. So, you’re going to have to set the paletteIndice value before you check what that value is to determine the color.

To add: ints are value types and not reference types. It doesn’t hold a reference to the value.

I would try changing

SwapColors (palettes[paletteIndice]);

to

SwapColors (palettes[chocoboManager.PaletteIndice]);

Then, just make sure you are setting the value to 1 before it’s called in your PaletteSwapper script. I’m not sure you want to do that in Start, personally, unless you are setting the value somewhere before the Start gets called.

Hi, thank you for you answer Brathnann :slight_smile:

I wasn’t aware of the differences between “value” and “reference” types, so I’ve bookmarked some pages and documentations to learn it later ^^.

Before seeing your post, I’ve partially found where the problem came from : I used

chocoboManager = gameObject.AddComponent(typeof(ChocoboManager)) as ChocoboManager

in the PaletteSwapper script, but it was already present on the Chocobo game object.
So I basically had a clone that was resetting the first one.

I changed “AddComponent” by “GetComponent”, and even if there is still a second visible reference in the Inspector, it’s on the PaletteSwapper Component itself now.

It wasn’t enough though, and by seeing you post, I changed to SwapColors (palettes[chocoboManager.PaletteIndice]); and get the paletteIndice out of the Awake() : it now works perfectly fine !

I still have the feeling that my code is ugly, unefficient and could be optimized as hell, but I will first review the basics, I have too much lack of knowledge and pratice to fix first :).

Again, thanks a lot for you quick help ! :smile: