- Home /
Mass instantiate causes corrupted strings on slower machines.
I'm instantiating about 6 playing cards with information on them. I know you are not supposed to instantiate a ton of thing at once and I plan on separating the instantiates with animations, however I noticed that when the game is run on a phone the strings show up corrupted. Armor for example will say something like Armr. This does not happen on my much faster PC. Only on Android does the bug appear. Is this a bug with unity?? It sure seems like it to me. I am not doing anything special with these strings I'm just setting them = to the text holder on the card.
I agree this really does make no sense
here is the script for opening the card the filed that comes out wrong is Card.type.text
public UICard OpenUnitCard(BoardManager.compressedUnit u){
GameObject g = Instantiate (largeCard) as GameObject;
g.transform.SetParent (packOpeningCanvas.transform);
Image backImage = g.GetComponent<Image> ();
RectTransform recTran = g.GetComponent<RectTransform> ();
g.transform.localScale = new Vector3 (1.1f, 1.1f, 1.1f);
recTran.anchoredPosition= new Vector2 (.5f, .5f);
recTran.rect.Set (0.5f, 0.5f, recTran.rect.width, recTran.rect.height);
BoardManager.compressedUnit stats = u;
UICard card = g.GetComponent<UICard> ();
if (u.unitClass == 1) {
backImage.color = new Color (1f, .7f, .7f, 1); // light red
}
if (u.unitClass == 2) {
backImage.color = new Color (1f, .9f, .7f, 1); // light yellow
}
if (u.unitClass == 3) {
backImage.color = new Color (.6f, .6f, 1, 1); // light blue
}
if (u.unitClass == 4) {
backImage.color = new Color (.7f, 1, .7f, 1); // light green
}
if (u.unitClass == 5) {
backImage.color = new Color (.85f, .6f, 1, 1); // light purple pink
}
card.des.text = stats.description;
card.Type.text = stats.type;
card.character.sprite = UnitIDMaker.instance.GetUnitTypeByID(u.ID).GetComponentInChildren<SpriteRenderer>().sprite; // this is to show you the unit
card.health.text = System.Math.Round(stats.maxHealth,1).ToString ();
card.accuracy.text = System.Math.Round(stats.accuracy,1).ToString ();
card.attackSpeed.text = System.Math.Round(1/stats.atkSpeed,1).ToString (); // convert to attacks per second
if (stats.dmg > 0) {
card.attack.text = "M" + System.Math.Round (stats.dmg, 1).ToString ();
}
if (stats.rangDmg > 0) {
card.attack.text = "R" + System.Math.Round (stats.rangDmg, 1).ToString ();
}
card.critChance.text = System.Math.Round(stats.critChance,1).ToString ();
card.defense.text = System.Math.Round(stats.defense,1).ToString ();
if(stats.isMelee){
card.critDamage.text = System.Math.Round(stats.fullMeleeCrit,1).ToString ();
}
else{
card.critDamage.text = System.Math.Round(stats.fullRangedCrit,1).ToString ();
}
if (stats.eqSpotTypes.Count > 0) {
card.EqIcon1.sprite = getEquipmentTypeImage (stats.eqSpotTypes [0]);
card.EqIcon1.gameObject.SetActive (true);
if (stats.eqSpotTypes.Count > 1) {
card.EqIcon2.sprite = getEquipmentTypeImage (stats.eqSpotTypes [1]);
card.EqIcon2.gameObject.SetActive (true);
if (stats.eqSpotTypes.Count > 2) {
card.EqIcon3.sprite = getEquipmentTypeImage (stats.eqSpotTypes [2]);
card.EqIcon3.gameObject.SetActive (true);
if (stats.eqSpotTypes.Count > 4) {
card.EqIcon4.sprite = getEquipmentTypeImage (stats.eqSpotTypes [3]);
card.EqIcon4.gameObject.SetActive (true);
} else {
card.EqIcon4.gameObject.SetActive (false);
}
} else {
card.EqIcon3.gameObject.SetActive (false);
card.EqIcon4.gameObject.SetActive (false);
}
} else {
card.EqIcon2.gameObject.SetActive (false);
card.EqIcon3.gameObject.SetActive (false);
card.EqIcon4.gameObject.SetActive (false);
}
} else {
card.EqIcon1.gameObject.SetActive (false);
card.EqIcon2.gameObject.SetActive (false);
card.EqIcon3.gameObject.SetActive (false);
card.EqIcon4.gameObject.SetActive (false);
}
return card;
}
This code never causes any issues except when its used in this function at the begging of the game
public void openStartPack(){
open1Unit (1); // common
open1Unit (1); // common
open1Unit(1); // common
open1Unit (1,2); // ucommon
open1Unit (1,2); // uncommon
open1Unit (1,2); // uncommon
open1Unit (1,3,3); // uncommon
open1Unit (1,3,4); // uncommon
open1Unit (1,3); // Rare
openAnEquipment(1);
openAnEquipment (1);
openAnEquipment (1);
GameManager.instance.gold += 100;
GameManager.instance.currentCampaignMission = 1;
}
Every Time i call open1unit a unit is spawned. This unit rolls stats which does not include assigning the string that we are getting corrupted. That string is set by default in the prefab in the inspector for the unit or item, and it is never changed IE a knight prefab will always have a type of knight. After it does that it calls the card opening function passes its stats on and destroys its self.
Like I said this is the only time it causes an issue and after the unit is received opening its card later will show the correct string even it was initially corrupted this means it is only displaying incorrectly in the text element but not saving incorrectly. It also only happens on the android.
So the cards have UI.Text Components, right? Are you sure that its not a typo anywhere?
Yes exactly they are UI text components and I and setting the string after instantiating them. I know it's not a typo because it's only happening on Android. It works perfectly on PC.
Can you post the code you're using for instantiating the objects as well as the resulting text object? This really makes no sense.
It really doesn't make sense your right. I'm not so concerned with my game being buggy as much as unity possibly having a bug because i can easily fix this with a work around. I would however prefer to know why it is happening before I do. Anyway I edited the question to include the code. Thanks for taking a look ritoban.
Sounds like a bug to me where the operation takes too long, but the result of something like Armor co$$anonymous$$g as Armr sounds really strange to me.
It should work like you have done it, but have you tried pooling which would be much better approach anyway? You could Instantiate stack of cards and GetComponents for them at the start of the game and then just edit them when showing. Having Instantiate and four GetComponents with multiple cards might be somewhat heavy process.
Your answer
Follow this Question
Related Questions
Remove already published achievements - Google Play Store 2 Answers
Failure [INSTALL_FAILED_CPU_ABI_INCOMPATIBLE] (solved) 1 Answer
Why does my pause & quit menus appear when I load next level? 0 Answers
I am not able to get the currentActivity,and i am getting the following error 0 Answers
why unity IPA file size increased after installation as compare to unity APK? 1 Answer