Quantcast
Channel: Questions in topic: "crash"
Viewing all articles
Browse latest Browse all 2383

When shooter script is attached and I hit play, Unity crashes?

$
0
0
Whenever my 'LaserShooter' script is attached and I press play to test, I get the spinning wheel (on my mac). I have to force quit Unity and restart. Here is the script that causes the crash/lag: using System.Collections; using System.Collections.Generic; using UnityEngine; public class LaserShooter : MonoBehaviour { public GameObject projectile; private int modChoose; private float speed = 50; private bool access; Vector3 rayOrigin = new Vector3(); Vector3 direction = new Vector3 (0, 0, -1); Vector3 pos1 = new Vector3 (-7, 2, 12); Vector3 pos2 = new Vector3 (1, 2, 12); Vector3 pos3 = new Vector3 (7, 2, 12); Quaternion spawnRotation = Quaternion.Euler(90,0,0); void Start () { modChoose = Random.Range (1,4); access = true; spawn (); //modChoose is picking which module to shoot from. } void spawn () { if (access == true) { if (modChoose == 1) { GameObject bullet = Instantiate (projectile, pos1, spawnRotation) as GameObject; bullet.GetComponent ().AddForce (-transform.forward * speed); rayOrigin = pos1; checkAccess (); //If the first module is picked, fire a bullet from a specific pos. } if (modChoose == 2) { GameObject bullet = Instantiate (projectile, pos2, spawnRotation) as GameObject; bullet.GetComponent ().AddForce (-transform.forward * speed); modChoose = Random.Range (1, 4); rayOrigin = pos2; checkAccess (); } if (modChoose == 3) { GameObject bullet = Instantiate (projectile, pos3, spawnRotation) as GameObject; bullet.GetComponent ().AddForce (-transform.forward * speed); modChoose = Random.Range (1, 4); rayOrigin = pos3; checkAccess (); } } } void checkAccess() { while (Physics.Raycast (rayOrigin, direction, 30)) { access = false; } access = true; selectRandom (); } void selectRandom() { spawn (); } }

Viewing all articles
Browse latest Browse all 2383

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>