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 !
- The tutorial used for the PaletteSwapper is “Unity 5: 2D Emulate Palette Swapping for Sprites”.