I do know a few details about it. I am trying to instantiate a prefab which creates an overlap sphere and when the overlapsphere hits a player, that’s when everything crashes.
So the problem is, how do I debug this? My code always looks perfectly fine to me but I know it isn’t lol. I might not be the best coder but with a few hours I can usually debug my problems. Also, can this damage my project in any way? Should I create a backup?
Here’s the code if anyone’s interested:
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour {
private int expireTime = 10;
private int projectileDamage = 20;
private float cooldown = 2;
private float cooldownBetweenProjectiles = 0.1f;
public GameObject explosion;
public string creator;
private PlayerStats statsScript;
public int health = 50;
private int radius = 100;
private Collider [] hitObjects;
private bool aquireTarget = true;
private bool fire = true;
private float fireUntil = 0;
private float fireDuration = 1;
private bool fireNextProjectile = true;
public GameObject projectile;
private float randomRotationX = 25;
private float randomRotationY = 25;
private Transform target;
private Vector3 firePosition;
private Quaternion fireRotation;
// Use this for initialization
void Start ()
{
//Destroy the object after a while
StartCoroutine(expire());
target = null;
}
// Update is called once per frame
void Update ()
{
if(health <= 0)
{
//Destroy the object if it loses all it's HP
destroy();
}
if(aquireTarget == true)
{
//If a target needs to be aquired, create an overlap sphere and call the chooseTarget function
hitObjects = Physics.OverlapSphere(transform.position, radius);
chooseTarget();
aquireTarget = false;
}
if(target != null)
{
//If there is a target, rotate towards it
transform.LookAt(target.position);
if(fire == true)
{
//If the turret is allowed to fire, do so
fireProjectiles();
fire = false;
}
}
}
void chooseTarget()
{
//Reset the target first
target = null;
for(int i = 0; i < hitObjects.Length; i++)
{
//If the target is a player, the function is finished.
if((hitObjects*.transform.tag == "Player1" && creator == "Player2") ||*
_ (hitObjects*.transform.tag == “Player2” && creator == “Player1”))_
_ {_
_ target = hitObjects.transform;
break;
}*_
* //If there’s no player in range pick the closest enemy turret.*
_ else if(hitObjects*.transform.tag == “Turret” &&
((hitObjects.GetComponent().creator == “Player1” && creator == “Player2”) ||
(hitObjects.GetComponent().creator == “Player2” && creator == “Player1”)))
{
target = hitObjects.transform;
//Going to put distance code and sorting here later on…
}
}
}*_
* IEnumerator CD()*
* {*
* //Cooldown until next burst of projecetiles*
* yield return new WaitForSeconds(cooldown);*
* fire = true;*
* }*
* IEnumerator smallCD()*
* {*
* //Cooldown between every projectile fired*
* yield return new WaitForSeconds(cooldownBetweenProjectiles);*
* fireNextProjectile = true;*
* }*
* void fireProjectiles()*
* {*
* //Start cooldown until next burst*
* StartCoroutine(CD());*
* //Set an amount of time for which the turret is allowed to fire*
* fireUntil = Time.time + fireDuration;*
* //Fire for the duration of time*
* while(Time.time < fireUntil)*
* {*
* //This is set to true after a small amount of time*
* if(fireNextProjectile == true)*
* {*
* //Start projectile cooldown*
* StartCoroutine(smallCD ());*
* fireNextProjectile = false;*
* //Fire position in front of the turret and a slightly randomized aim*
* firePosition = transform.TransformPoint(0, 0, 1);*
* fireRotation = randomize(Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0)));*
* //Instantiate the projectile for all players*
* networkView.RPC(“fireProjectile”, RPCMode.All, firePosition, fireRotation, creator);*
* }*
* }*
* }*
* IEnumerator expire()*
* {*
* //Destroy the object after an amount of seconds*
* yield return new WaitForSeconds(expireTime);*
* destroy();*
* }*
* void destroy()*
* {*
* //Create an explosion and destroy the object*
* Instantiate(explosion, transform.position, transform.rotation);*
* Destroy(gameObject);*
* }*
* [RPC]*
* void fireProjectile(Vector3 position, Quaternion rotation, string createdBy)*
* {*
* //Instantiate projectile and set damage and creator in it’s script*
* GameObject p = Instantiate(projectile, position, rotation) as GameObject;*
* p.GetComponent().setDamage(projectileDamage);*
* p.GetComponent().setCreator(createdBy);*
* }*
* Quaternion randomize(Quaternion rot)*
* {*
* //Modify the rotation slightly*
* float randomX = Random.Range(-randomRotationX / 2, randomRotationX / 2);*
* float randomY = Random.Range(-randomRotationY / 2, randomRotationY / 2);*
* return Quaternion.Euler(new Vector3(rot.eulerAngles.x + randomX, rot.eulerAngles.y + randomY, rot.eulerAngles.z));*
* }*
}