- Home /
runtime: script variable not processed as demanded
hi all,
i try to make a candy-crush-like game.
i have make some sprites and design them in 5x5. i have attach them a script that show the sprite name and their "game value" in script property.
when i run, i notice that not all my 5x5 sprites have done the script, they just show 0 value. anyone can help me? here is the code snippet:
public float posisibaris=0; public float posisikolom=0; public int jenisunit=0; public int nilai=0;
// Use this for initialization
void Start () {
BlocksUnitScript gameobj = GetComponent<BlocksUnitScript>();
posisibaris = gameobj.transform.position.x;
posisikolom = gameobj.transform.position.y;
jenisunit = get_jenisunit ();
nilai = get_nilai ();
}
private int get_nilai(){
int returnvalue = 0;
switch (jenisunit) {
case 0 :
returnvalue=0;
break;
case 1 : //berry
returnvalue=50;
break;
case 2 : //grape
returnvalue=30;
break;
case 3 : //orange
returnvalue=45;
break;
case 4 : //strawberry
returnvalue=100;
break;
case 5 : //apple
returnvalue=75;
break;
case 6 : //melon
returnvalue=300;
break;
case 7 : //pear
returnvalue=500;
break;
default :
returnvalue=0;
break;
}
return returnvalue;
}
private int get_jenisunit(){
int returnindex = 0;
SpriteRenderer gameobj_sprite = GetComponentInChildren<SpriteRenderer> ();
if (GlobalVarScript.mySprites != null ) {
for (int i=0;i<GlobalVarScript.mySprites.Length; i++){
if (gameobj_sprite.sprite==GlobalVarScript.mySprites[i]) {
returnindex=i+1;
break;
}
}
}
gameobj_sprite = null;
return returnindex ;
}
Answer by golddevrush · Jan 03, 2015 at 03:35 PM
i have found the solution. the problem is : i write the function of loading sprite from resources in void start. and the function get_nilai is quicker invoked before the function of loading finished, so not all my sprite got the actual value. when i change the location of the function of loading to void awake, it solve the problem.
Your answer
Follow this Question
Related Questions
Help with changing spririterenderer's sprite 2 Answers
is there any 4 or 8 direction script? for sprite maneger 2 0 Answers
how to press 2 sprites to display other 1 sprite? 1 Answer
How do I replace a sprite in script? 2 Answers
Unknown Error. 3 Answers