Search Unity

Resolved trying to make my turret target the closest enemy and then rotate to face it results in error

Discussion in 'Scripting' started by ZipperGE19, Nov 29, 2022.

  1. ZipperGE19

    ZipperGE19

    Joined:
    Nov 6, 2022
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Turret : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public float speed = 1.0f;
    9.     void OnTriggerStay(Collider col)
    10.     {
    11.         target = FindClosestEnemy();
    12.         Vector3 targetDirection = target.position - transform.position;
    13.         float singleStep = speed * Time.deltaTime;
    14.         Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
    15.         Debug.DrawRay(transform.position, newDirection, Color.red);
    16.         transform.rotation = Quaternion.LookRotation(newDirection);
    17.     }
    18.     public GameObject FindClosestEnemy()
    19.     {
    20.         GameObject[] gos;
    21.         gos = GameObject.FindGameObjectsWithTag("Human");
    22.         GameObject closest;
    23.         closest = null;
    24.         float distance = Mathf.Infinity;
    25.         Vector3 position = transform.position;
    26.         foreach (GameObject go in gos)
    27.         {
    28.             Vector3 diff = go.transform.position - position;
    29.             float curDistance = diff.sqrMagnitude;
    30.             if (curDistance < distance)
    31.             {
    32.                 closest = go;
    33.                 distance = curDistance;
    34.             }
    35.         }
    36.         return closest;
    37.     }
    38. }
    Turret.cs(11,18) Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Transform'
    I do not understand what is wrong or how to fix this and google isnt turning up any results
    theres no actual weapon for the turret yet. I am just trying to do this in smaller bite sized steps as I am not proficient in c#. This current script is trying to search for the closest player/NPC, and then it will rotate the weapon (a dish thats supposed to represent a parabolic mirror) to aim at its target
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    So you're saying you don't know the difference between a Transform component type and a GameObject type? They're completely different things i.e. types. Square peg, round hole. :)

    It tells you Line 11, Column 18. You only then have to look at what "target" is and see you've defined it as a Transform. You then look at your "FindClosestEnemy()" method and see it doesn't return a Transform but a GameObject.
     
  3. ZipperGE19

    ZipperGE19

    Joined:
    Nov 6, 2022
    Posts:
    6
    my brain didnt catch this and I realized what was wrong
    fixed it by changing target = FindClosestEnemy(); to target = FindClosestEnemy().transform; and it worked perfectly