Search Unity

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

Discussion in 'Scripting' started by Deleted User, May 1, 2022.

  1. Deleted User

    Deleted User

    Guest

    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:
    11,789
    You’re missing a .y
     
    mopthrow likes this.
  3. Deleted User

    Deleted User

    Guest

    I'm an idiot.
    Thank you.
     
  4. AcidArrow

    AcidArrow

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

    Prazo20

    Joined:
    Jul 9, 2022
    Posts:
    10
    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:
    38,726
    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:
    10
    YH thanks Kurt...