Search Unity

Question Please help me with this problem!!!!

Discussion in 'Scripting' started by ASDF_alpha, Jul 2, 2021.

  1. ASDF_alpha

    ASDF_alpha

    Joined:
    Aug 25, 2020
    Posts:
    29
    I am creating a tower defence game and i am creating a turret script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Turret : MonoBehaviour
    7. {
    8.     public LineRenderer laser;
    9.     public Transform Head;
    10.     public float Enemydistance;
    11.     public float range = 15;
    12.     public float rotationSpeed = 10f;
    13.     public float nearestEnemyDistance;
    14.     public GameObject nearestEnemy = null;
    15.     public bool targetinRange = false;
    16.     GameObject target = null;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         InvokeRepeating("findEnemy", 0f, 0.5f);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (nearestEnemyDistance <= range)
    28.         {
    29.             targetinRange = true;
    30.         }
    31.         if (target = null)
    32.         {
    33.             return;
    34.         }
    35.         else if (target != null && targetinRange)
    36.         {
    37.             Vector3 dir = target.transform.position - transform.position;
    38.             Quaternion lookRotation = Quaternion.LookRotation(dir);
    39.             Vector3 rotation = Quaternion.Lerp(Head.rotation, lookRotation, Time.deltaTime * rotationSpeed).eulerAngles;
    40.             Head.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    41.         }
    42.     }
    43.  
    44.     public void findEnemy()
    45.     {
    46.         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    47.         foreach (GameObject Enemy in enemies)
    48.         {
    49.             nearestEnemyDistance = Mathf.Min(Enemydistance);
    50.             Enemydistance = Vector3.Distance(Enemy.transform.position, this.transform.position);
    51.             if (Enemydistance <= nearestEnemyDistance)
    52.             {
    53.                 nearestEnemyDistance = Enemydistance;
    54.                 nearestEnemy = Enemy;
    55.             }
    56.             if (nearestEnemy != null)
    57.             {
    58.                 target = nearestEnemy;
    59.             }
    60.             else
    61.             {
    62.                 return;
    63.             }
    64.         }
    65.     }
    66.     void OnDrawGizmosSelected()
    67.     {
    68.         Gizmos.color = Color.red;
    69.         Gizmos.DrawWireSphere(transform.position, range);
    70.     }
    71. }
    72.  
    I set the target = nearest enemy but target was null when i hit play.(nearest enemy is not null)
    as shown here: upload_2021-7-2_12-38-46.png Please help me.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    I didn't really read too much of it, but on line 31 you have
    if (target = null)
    , which should've thrown an error already. You need two equals signs if you're comparing the two.

    On line 49, you have
    nearestEnemyDistance = Mathf.Min(Enemydistance)
    , which doesn't make sense because you're getting the minimum value of 1 value, so it always returns itself. Why have Mathf.Min() if you're not using it?

    Also, can you try an re-explain what your actual problem is, I can't figure it out. Your code is all over the place and I honestly don't know where to start.
     
    Last edited: Jul 2, 2021
  3. ASDF_alpha

    ASDF_alpha

    Joined:
    Aug 25, 2020
    Posts:
    29
    target should not be null
     
  4. ASDF_alpha

    ASDF_alpha

    Joined:
    Aug 25, 2020
    Posts:
    29
    see line 58.
     
  5. ASDF_alpha

    ASDF_alpha

    Joined:
    Aug 25, 2020
    Posts:
    29
    it's ok you solved it.thx
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    On line 62, why are you returning? That exits out of the function completely.
     
  7. ASDF_alpha

    ASDF_alpha

    Joined:
    Aug 25, 2020
    Posts:
    29
    It’s ok I changed that line right after the post thx