Problems with Byte :S

I am working on a Genetic Algorithm but i have an annoying problem that i cant solve:

I have 3 variables

//BASIC MONSTER STATS
	public byte speed = 8;
	public byte size = 5;
	public byte health = 4;
	//---------------------

On start i do the next

void Start () {
		byte[] myBytes = new byte[3]{(byte)this.speed, (byte)this.size,(byte)this.health };
                Debug.LogError(myBytes[0]);
	}

And always return 1! :S

I tested with

void Start () {
		byte[] myBytes = new byte[3]{8, 4,3 };
                Debug.LogError(myBytes[0]);
	}

And this one return 8.

Any ideas?
Thanks

Your variables that you assign as array elements already are of type byte so no reason to cast them to byte:

(byte) this.speed

Instead:

speed

will suffice.

It shouldn’t affect the result though unless it’s something I don;t know about C# - there’s a lot I don;t know.
No other ideas.

what Pirs01 said…

also, since they are public variables, are you sure they dont get set to another value in the inspector or from another script?

I first tried without casting.

OMG i used private and worked!
Anyway i am 99% sure that this variables dont get another value :S

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
	public byte speed = 8;
	public byte size = 5;
	public byte health = 4;

	public void Start () {
		byte[] bytes = new byte[3] {
			speed,
			size,
			health
		};
		foreach (byte item in bytes) {
			Debug.Log(item.ToString());
		}
	}
}

output:

8
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/Scripts/Test.cs:16)
5
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/Scripts/Test.cs:16)
4
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/Scripts/Test.cs:16

They did get another value. When they are public, they are exposed to the Unity Inspector and its serialization. I would guess that at some point you set them, in the script or in the inspector, as 1, and Unity simply serialized that value. At that point, changing the value in the script meant nothing.

Whoa, byte actually works correctly in the inspector now…for the longest time it showed up as a boolean instead. :shock:

–Eric