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.

Question error CS0019: Operator '==' cannot be applied to operands of type 'Vector3' and 'float'

Discussion in 'Scripting' started by gabetehfrick, May 1, 2022.

  1. gabetehfrick

    gabetehfrick

    Joined:
    Feb 9, 2022
    Posts:
    2
    Hi, I'm trying to code a enemy bullet that targets the player's current location at the time of being created, travels to that location, and then deletes itself when it reaches that location.

    I've been following this tutorial:


    The code is practically identical to what is shown at 11:27

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyBullet : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private Transform player;
    10.     private Vector2 target;
    11.  
    12.     void Start()
    13.     {
    14.         player = GameObject.FindGameObjectWithTag("Player").transform;
    15.  
    16.         target = new Vector2(player.position.x, player.position.y);
    17.     }
    18.  
    19.  
    20.     void Update()
    21.     {
    22.         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    23.  
    24.         if (transform.position.x == target.x && transform.position == target.y)
    25.         {
    26.             DestroyProjectile();
    27.         }
    28.     }
    29.  
    30.     void OnTriggerEnter2D(Collider2D other)
    31.     {
    32.         if (other.CompareTag("Player"))
    33.         {
    34.             DestroyProjectile();
    35.         }
    36.     }
    37.  
    38.     void DestroyProjectile()
    39.     {
    40.         Destroy(gameObject);
    41.     }
    42. }
    I get the error message for Line 24 saying "error CS0019: Operator '==' cannot be applied to operands of type 'Vector3' and 'float' "

    There's no Vector3 in this script. I have no idea what the problem is.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,547
    You’re missing a .y
     
    mopthrow likes this.
  3. gabetehfrick

    gabetehfrick

    Joined:
    Feb 9, 2022
    Posts:
    2
    I'm an idiot.
    Thank you.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,547
    It’s all good, happens even to veterans.
     
  5. Prazo20

    Prazo20

    Joined:
    Jul 9, 2022
    Posts:
    5
    Thank you very much i too was having the same problem i had to redo y script but i just told myself ...bruh check the forum and boom right there the answer was there. Thanks Bruh...:)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    26,949
    Some help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyyy':"

    http://plbm.com/?p=263

    Super common error, super easy fix: understand what you are assigning, copying or passing around.
     
  7. Prazo20

    Prazo20

    Joined:
    Jul 9, 2022
    Posts:
    5
    YH thanks Kurt...
     
unityunity