game freezes on devices but plays well on pc.
Hi.i've build a game and it plays with no problems on pc and android simulator but when i test it on devices it freezes when the bullet prefab collides with the enemy.it goes down to 1-2 fps or smth or sometimes it randomly freezes.What can be the cause? devices tested:iphone 6s, samsung galaxy s6.
What can be the cause?
Poorly coded expectations? While loops that take forever to end? Etc.
Best to post your code as it relates to the collision.
this is the code for the collide (the pirate and the slash one work fine game freezes when the bullet collides)
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "pirate")
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
nikise = true;
Destroy(this.gameObject,0.3f);
Destroy(transform.parent.gameObject,0.3f);
}
if(col.gameObject== bullet)
{
tempHealth -= 1;
}
if (col.gameObject.name == "slash")
{
swordCol = true;
tempHealth -= 3;
}
}
(In the update i have this) bullet = GameObject.Find("Bullet(Clone)"); (i also tried "col.gameObject== Bullet(Clone)");
this code is in the bullet for it to be destroyed if it collides with the monkey
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.name == "monkey")
{
Destroy(this.gameObject);
}
}
You have the code bullet = GameObject.Find("Bullet(Clone)");
in the Update function?. That might be the issue because GameObject.Find can be super slow depending upon the number of objects present in hierarchy. And you're calling it in update, which makes things worse.
Consider using tags for finding gameObjects. And don't call it in Update function.
Answer by DimSour · Nov 10, 2016 at 01:45 PM
so the problem was my enemy's health bar script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MonkeyHealthBar : MonoBehaviour {
public float fillAmount;
public float tempHealth;
public float maxHealth;
public monkey monkeyScript;
public Image content;
void Update ()
{
tempHealth = monkeyScript.tempHealth;
maxHealth = monkeyScript.maxHealth;
fillAmount = tempHealth / maxHealth;
content.fillAmount = fillAmount;
}
}
does anyone know the reason?? @aditya007 @Landern
Your answer
Follow this Question
Related Questions
Unity 5 keeps freezing when I try to build my Android project. 1 Answer
Building .APK Apps using a Unity App 0 Answers
Help. About IPA size, my empty project have 46MB... 0 Answers
Problem with building on Android 0 Answers
Unity says that I'm missing my NDK and SDK libraries, but I have them installed via Unity hub? 1 Answer