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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Need help rotating a game object to the position of another

Discussion in 'Scripting' started by jojomonster679, Jan 22, 2019.

  1. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    Some background information: I am following a tutorial by popular unity youtuber Brackeys. I have followed it very closely (it is how to make a tower defense game) although I am running into the issue that although the debug.logs are telling me that everything is working, I get the error "NullReferenceException: Object reference not set to an instance of an object" and says the error is on line 54. The turret is also not rotating to the oncoming enemies position. Full code below:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Turret : MonoBehaviour
    5. {
    6.  
    7.     private Transform target;
    8.     public float range = 15f;
    9.  
    10.     public string enemyTag = "Baddy";
    11.  
    12.     public Transform partToRotate;
    13.  
    14.     private void Start()
    15.     {
    16.         InvokeRepeating("UpdateTarget", 0f, 0.5f);
    17.     }
    18.  
    19.     void UpdateTarget()
    20.     {
    21.         GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
    22.         float shortestDistance = Mathf.Infinity;
    23.         GameObject nearestEnemy = null;
    24.         //Debug.Log("Looking!");
    25.  
    26.         foreach (GameObject enemy in enemies)
    27.         {
    28.             //Debug.Log("checking");
    29.             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
    30.             if(distanceToEnemy < shortestDistance)
    31.             {
    32.                 shortestDistance = distanceToEnemy;
    33.                 nearestEnemy = enemy;
    34.                 //Debug.Log("its close!");
    35.             }
    36.         }
    37.  
    38.         if(nearestEnemy != null && shortestDistance <= range)
    39.         {
    40.             target = nearestEnemy.transform;
    41.             //Debug.Log("I see an enemy!");
    42.         }
    43.         else
    44.         {
    45.             target = null;
    46.         }
    47.     }
    48.  
    49.     private void Update()
    50.     {
    51.         if (target = null)
    52.             return;
    53.  
    54.         Vector3 dir = target.position - transform.position;
    55.         Quaternion lookRotation = Quaternion.LookRotation(dir);
    56.         Vector3 rotation = lookRotation.eulerAngles;
    57.         partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    58.  
    59.     }
    60.  
    61.     private void OnDrawGizmosSelected()
    62.     {
    63.         Gizmos.color = Color.red;
    64.         Gizmos.DrawWireSphere(transform.position, range);
    65.     }
    66. }
    67.  
    THANKS FOR ANY HELP!
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    On line 51, you have "if (target = null)", but you probably mean "if (target == null)", with two equals signs.

    == means test if two things are equal
    = means make them equal
     
  3. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    193
    Your mistake is on line 51, you are using an assignment operator =, rather than a comparison ==. Also, not a fan of that return to "break" from the Update(), simply do, instead : if (target != null) {//Rest of the Update() code.}
     
    jojomonster679 likes this.
  4. jojomonster679

    jojomonster679

    Joined:
    Oct 19, 2018
    Posts:
    14
    THANK YOU