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

Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Transform

Discussion in 'Scripting' started by Anon1234, Apr 23, 2015.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Error I got is:
    error CS0029: Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Transform

    The line it's reffering to is:
    Transform arriveAt = new Vector2(rotVec.x, rotVec.y);


    Tried to fix it with:

    Transform arriveAt = new (Transform)Vector2(rotVec.x, rotVec.y);
    Ended up with
    "Unexpected symbol ("

    Transform arriveAt = (Transform)Vector2(rotVec.x, rotVec.y);
    Ended up with
    error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Well, a Transform is not a Vector2. Don't worry, I can get you on the right track. Try this.


    Code (CSharp):
    1. Vector2 arriveAt = new Vector2(rotVec.x, rotVec.y);
    2.  
    3. Transform myTransform = //Get a reference to the transform you want to modify here
    4.  
    5. myTransform.position = arriveAt;
    6.  
    7.  
    The issue is that you are trying to assign a Vector2 to a Transform variable. These are two different types and they are not interchangeable. What you want to do is get a reference to the transform you are trying to modify and them modify the parameters accordingly e.g. rotation, position, localScale, etc.

    Hope this steers your in the right direction! Let me know if you need further clarification. Posting your code would be helpful.
     
    Anon1234 likes this.
  3. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    I see what you're attempting, which is a great way to solve it.
    But "arriveAt" is used in "Vector2.Distance" and it doesn't accept Vector2 but transform.position. arriveAt has to be Vector2 for movement with lerp (I'll do lerp movement myself) and transform.position to compare the distances. That's why it was meant to be Transform in the first place. Because Transform can be easily converted to Vector2.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you say that arriveAt has to be a Vector2, why do you declare it to be a Transform?
    Code (csharp):
    1. Transform arriveAt = new Vector2(rotVec.x, rotVec.y);
    If you want it to be a Vector2, it needs to be:
    Code (csharp):
    1. Vector2 arriveAt = new Vector2(rotVec.x, rotVec.y);
     
    Anon1234 and hamsterbytedev like this.
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    A Transform CANNOT be converted to a Vector2. It's impossible. The position of a Transform can be converted to a Vector2, though it is actually a Vector3, but the Transform itself cannot. Without seeing your implementation it will be difficult to solve your specific problem. What exactly are you trying to do with this Transform?

    Vector2.Distance absolutely does accept a Vector2...it requires two of them and returns a float. Usage is like this:

    Code (CSharp):
    1. float dist = Vector2.Distance(V1, V2);
    transform.position is also a Vector2; not a transform.

    Don't worry, you'll get the hang of this! Just stick with it and don't get discouraged. Learning to write code is not easy.
     
    datweaks, Anon1234 and Dantus like this.
  6. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    If you say that arriveAt has to be a Vector2, why do you declare it to be a Transform?
    Because it would need to be transform for distance check.

    Learning to write code is not easy.
    o_O,
    I'm actually C#/Java/PHP programmer, I'm just not used to your API, such things as "Transform" and "Vector2" are not used in VC+ or Qt or anything I've seen before.

    Code (CSharp):
    1. float dist = Vector2.Distance(V1, V2);
    But then, public GameObject Player; would need to be converted to Vector2. But I somehow try to manage the code, I got the Vector of player, applied it to distance check, but then there's another error, involved code:

    Code (CSharp):
    1. Vector2 vec = new Vector2(Player.transform.position.x + KeepDistance, Player.transform.position.y);
    2.         // Get the desired distance.
    3.  
    4.         Quaternion rot = Quaternion.Euler(0, 0, HardToFollow);
    5.         // Get the desired angle.
    6.  
    7.         Vector2 rotVec = rot * vec;
    8.         // Get Vector of the position
    9.  
    10.         transform.position = new Vector2(rotVec.x, rotVec.y);
    11.         // Attempt movement to the Vector2.
    12.  
    13.         Vector2 arriveAt = Vector2(rotVec.x, rotVec.y);
    14.         // The targetting of the place where you want to go.
    15.  
    The error: error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected.
    But then again, I could also make a new Vector2() out of it.

    Problem solved, thank you for your contribution.
     
    hamsterbytedev likes this.
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    hamsterbytedev likes this.
  8. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    This has been already said 3 times, thank you very much! I got this by now!
     
  9. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I do suggest checking the docs before posting a question on here. I wasn't trying to demean your abilities but the way you were phrasing things lead me to believe that you were a novice programmer with no experience in C# or Unity.

    When you have to be told that a Transform is not a Vector2 it does indicate that you have not read the docs and that you are a novice programmer. The sky is not the ocean; it's the sky. A dog is not a cat; it's a dog. A Transform is not a Vector2; it's a Transform. C# has strict standards and things often have to be declared explicitly because no implicit conversion exists. I mean no offence by this of course; I'm just providing some hyperbolic simile. If you are more comfortable with Java, Unity does accept Javascript as well which is a little looser with standards. Though, you still can't call a Transform a Vector2 and make it so.

    That's entirely wrong. The distance check takes Vector2 parameters, not Transform parameters. Moreover, a GameObject is not a Vector2 either, but gameObject.transform.position is.

    http://docs.unity3d.com/ScriptReference/Vector2.Distance.html

    I suggest you take some time to familiarize yourself with the API
     
  10. Koni2

    Koni2

    Joined:
    May 25, 2020
    Posts:
    1
    huhyah it happens to me too but i used another script