Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am currently running into this error:

Cannot implicitly convert type UnityEngine.Vector3 to Unity.Engine.Transform

I am not sure on how to fix this issue because, here's the code where the issue is occurring.

target = pickedNumber.transform.position;
ball.transform.position = Vector3.MoveTowards(transform.position, target.position, step);

Here's the for loop that contains the above snippet:

float waitTime = 8;
    float step = speed * Time.deltaTime * 2;

    yield return new WaitForSeconds (waitTime);

    for (int i = 0; i <= 19; i++) {
        //Finding Game Objects
        numbers = GameObject.FindGameObjectsWithTag ("num");
        ball = GameObject.FindGameObjectWithTag ("ball");

        //Picking the random cube
        index = Random.Range (0, numbers.Length);
        pickedNumber = numbers [index];

        //Moving ball to cube
        target = pickedNumber.transform.position;
        ball.transform.position = Vector3.MoveTowards(transform.position, target.position, step);

        //Pause here
        yield return new WaitForSeconds (waitTime);

        //Ball Returns to position
        ball.transform.position = new Vector3 (0, 0, 0);
    }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

If target is a transform, you want it to be target = pickedNumber.transform;, (no transform.position).

If target is a Vector3, you want the next line to be ball.transform.position = Vector3.MoveTowards(transform.position, target, step);, (no target.position).

I assume it's the first one, because of the wording of the error, but the second one is to show that it could be fixed another way.

share|improve this answer
    
That didn't work still getting the same issue with a new one about overloading the function. The best overloaded method match for UnityEngine.Vector3.MoveTowards(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments and Argument #2' cannot convert UnityEngine.Transform' expression to type UnityEngine.Vector3 – user3727417 Apr 21 at 15:34
    
Only make one of those changes. If target is a transform, you still need "target.position" in the second line. I'll edit to make it more clear. – Jibb Smart Apr 21 at 15:35
    
that worked thank you! – user3727417 Apr 21 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.