So I have a game where the player can fire from a turret by touching the screen. When I play the game in Unity the turret fires just fine, but when I deploy it to my iPhone the turret will fire but the beam it shoots disappears immediately. I thought it might be the beam colliding with the turret itself, but turning off all the collisions on the beam had no effect. Any idea what could be causing the problem? The beam will be visible for a split second, but immediately vanishes.
I tried creating a separate sorting layer for the beam but I didn't see a difference. I also tried coding the turret so that the beam actually starts slightly in front of the turret, but no luck there either.
Unity version: 5.4.1f1
iPhone 6, iOS 10
Coded on a Macbook 2011, using latest XCode for deployment
Here's the code that detects the touch (it's inside an update function):
if (Input.touchCount > 0 && Input.GetTouch(0).phase==TouchPhase.Began) {
RaycastHit2D rh = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
if (rh.collider != null) {
if (rh.transform.gameObject.name == "Cannon")
Cannonscript.fire(laserSpeed);
}
}
And here's the fire function that's inside Cannonscript (the scale variable is either -1 or 1 and determines if we're firing left or right):
public void fire (float speed) {
speed = scale * speed;
GameObject g = (GameObject)Instantiate (laser, new Vector3(transform.position.x + scale * 0.9f, transform.position.y, transform.position.z), Quaternion.identity);
g.GetComponent().fire(speed);
}
And the code from LaserScript that makes it fly through the air:
public void fire (float speed) {
GetComponent().velocity = new Vector2(speed, 0f);
}
Finally, here's the properties of the laser: http://i.imgur.com/UMXcPHZ.png
Note that the laser has its own sorting layer. The cannon is on the Default layer.
Any help is much appreciated. Let me know if you need more information. Thanks!