- Home /
Question by
AmarKalabic · Feb 04, 2015 at 03:24 PM ·
javascriptvariablescriptingbasicsenum
Variable doesn't output what it should?
I've been making an block game for a while but now I'm stuck. I've problem with enums, which I use to store types of blocks. No matter what I do or change, my "Type" variable of every block is "block 7". It's strange because every block has it's own sprite, but block type is bugged and showing "block 7" for every one of them.
//for (var object : GameObject in go_list) { // I tried using for loop too, same result
var go_list = GameObject.FindGameObjectsWithTag("blok");
var l = go_list.length;
var i : int = 0;
//Debug.Log(go_list.length); returns 768, which is okay, since I have 3 chunks, 16x16 = 256; 256x3 = 768 blocks
while (i <= l) {
var object = go_list[i];
i += 1;
if (object.transform.position.y < world.chunkHeight - 1) {
type = BlockType.block1;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(0, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
if (object.transform.position.y >= world.chunkHeight - 1) { //bilo gameObject umjesto object
if (object.transform.position.y >= world.chunkHeight - world.chunkHeight/2) {
type = BlockType.block2;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(25, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
}
if (object.transform.position.y < world.chunkHeight - world.chunkHeight/2) {
if (object.transform.position.y >= world.chunkHeight - world.chunkHeight/2 - world.chunkHeight/4) {
var prob_a : int = Random.Range(1,12);
if (prob_a == 1) {
type = BlockType.block4;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(50, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
else if (prob_a == 2) {
type = BlockType.block5;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(0, 150, 25, 25), Vector2(0.5, 0.5), 25);
}
else {
type = BlockType.block3;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(50, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
}
}
if (object.transform.position.y < world.chunkHeight - world.chunkHeight/2 - world.chunkHeight/4) {
if (object.transform.position.y >= world.chunkHeight - world.chunkHeight + 1) {
var prob_b : int = Random.Range(1,24);
if (prob_b == 1) {
type = BlockType.block6;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(0, 125, 25, 25), Vector2(0.5, 0.5), 25);
}
else {
type = BlockType.block3;
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(50, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
}
}
if (object.transform.position.y < world.chunkHeight - world.chunkHeight + 1) {
Debug.Log(object.transform.position.y + " " + world.chunkHeight);
type = BlockType.block7; //block7 is bottom block
object.GetComponent(SpriteRenderer).sprite = Sprite.Create(Resources.Load("Sprites/blocks") as Texture2D, Rect(75, 175, 25, 25), Vector2(0.5, 0.5), 25);
}
}
}
enum BlockType {block1, block2, block3, block4, block5, block6, block7};
var type : BlockType;
P.S. Block 7 is bottom block and it's rendered and spawned last (which maybe could be source of this problem?).
Thanks in advance!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613182436im_/https://answers.unity.com/themes/thub/images/avi.jpg)