Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Script not work properly when set on 2 objects.

Discussion in 'Scripting' started by psykick1, May 23, 2020.

  1. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hi

    There is 2 turrets.
    1. Standart
    2. Laser.

    only standart is working.

    But when I play with the code and change places of the functions.
    the laser is work and the standart no.

    Please help.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Turret : MonoBehaviour
    5. {
    6.     private Transform target;
    7.     public float range = 20f;
    8.     public string enemyTag = "Enemy";
    9.     public Transform partToRotate;
    10.     public  float distanceToEnemy;
    11.     public float shortestDistance;
    12.     public float turnSpeed = 10f;
    13.     public float countDown = 1f;
    14.     private float fireRate = 45f;
    15.     public GameObject bulletPrefab;
    16.     public Transform bulletLocation;
    17.     public static int cost = 400;
    18.     public LineRenderer LineRend;
    19.     public bool laser = false;
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         InvokeRepeating("UpdateTarget", 0f, 0.1f);
    24.         shortestDistance = Mathf.Infinity;
    25.     }
    26.     void UpdateTarget()
    27.     {
    28.         target = null;
    29.         GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
    30.         GameObject currentEnemy = null;
    31.      
    32.         foreach (GameObject enemy in enemies)
    33.         {
    34.             distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
    35.             if (distanceToEnemy < range && currentEnemy == null)
    36.             {
    37.                 currentEnemy = enemy;
    38.                 target = enemy.transform;
    39.             if (distanceToEnemy > range)
    40.                 {
    41.                     target = null;
    42.                 }
    43.            
    44.             }
    45.        
    46.         }
    47.      
    48.      
    49.     }
    50.     // Update is called once per frame
    51.     void Update()
    52.     {
    53.         countDown -= 1f / fireRate;
    54.         if (countDown <= 0f)
    55.         {
    56.             Shoot();
    57.             countDown = 2;
    58.         }
    59.         Vector3 dir = target.position - transform.position;
    60.         Quaternion lookRotation = Quaternion.LookRotation(dir);
    61.         Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
    62.         partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    63.         if (target == null)
    64.         {
    65.             LineRend.enabled = false;
    66.         }
    67.         if (target !=null)
    68.         {
    69.             LineRend.enabled = true;
    70.             LaserShoot();
    71.         }
    72.     }
    73.     void LaserShoot ()
    74.     {
    75.             LineRend.SetPosition(0, bulletLocation.position);
    76.             LineRend.SetPosition(1, target.position);
    77.     }
    78.     void Shoot()
    79.     {
    80.         GameObject BulletGO = (GameObject)Instantiate(bulletPrefab, bulletLocation.position, bulletLocation.rotation);
    81.         Bullet bullet = BulletGO.GetComponent<Bullet>();
    82.             bullet.Seek(target);
    83.             bullet = null;
    84.     }
    85.     void OnDrawGizmosSelected()
    86.     {
    87.         Gizmos.color = Color.red;
    88.         Gizmos.DrawWireSphere(transform.position, range);
    89.     }
    90. }
     
  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Not directly related to the question but this line:
    countDown -= 1f / fireRate;
    should be made framerate-dependant by multiplying by Time.deltaTime (like this
    countDown -= 1f / fireRate * Time.deltaTime;
    ).

    This whole block should be wrapped in some sort of conditional.

    Code (CSharp):
    1. Vector3 dir = target.position - transform.position;
    2.         Quaternion lookRotation = Quaternion.LookRotation(dir);
    3.         Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
    4.         partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    Currently if you don't have a target it's going to throw a null reference error since it's trying to pull from target even if it's null. So go ahead and put it inside a conditional like
    if (target)
    or
    if (target != null)
    (they both mean the same thing). I'm guessing right now the laser's linerenderer isn't shutting off?

    Also how are you differentiating between a turret that is a laser turret and a turret that shoots bullets? Currently it's trying to run both versions of the turret so if your regular canon doesn't have a LineRenderer or your laser turret doesn't have a bulletPrefab you're gonna run into more null reference errors that will cancel script execution and cause it to not work.

    So wrap this chunk of code in a conditional like
    if (bulletPrefab != null)


    Code (CSharp):
    1. countDown -= 1f / fireRate;
    2.         if (countDown <= 0f)
    3.         {
    4.             Shoot();
    5.             countDown = 2;
    6.         }
    and this chunk of code in a conditional like
    if (LineRend != null)
    .

    Code (CSharp):
    1. if (target == null)
    2.         {
    3.             LineRend.enabled = false;
    4.         }
    5.         if (target !=null)
    6.         {
    7.             LineRend.enabled = true;
    8.             LaserShoot();
    9.         }