- Home /
Cannot convert `UnityEngine.Transform' expression to type `UnityEngine.Vector3'(Solved)
Trying to make a script that will enable a game object to follow a set of waypoints. But getting error -
Assets/WaypointInvaderMove.cs(15,50): error CS1503: Argument #2' cannot convert
UnityEngine.Transform' expression to type `UnityEngine.Vector3'
Script -
using UnityEngine;
using System.Collections;
public class WaypointInvaderMove : MonoBehaviour
{
public Transform[] movePositions; // Create array(list) of positions/co-ords
int moveToIndex = 0;
public float speed = 5f;
void Update()
{
if(moveToIndex < movePositions.Length) //if moveToIndex is less than total array items/index do...
{
Vector3 newPos = Vector3.MoveTowards(transform.position, movePositions[moveToIndex], speed * Time.deltaTime);
transform.position = newPos;
if(newPos == movePositions[moveToIndex]) //if current position = the same as array index do...
{
moveToIndex++; // move to the next array index position
}
}
}
}
How do I fix this problem ?
???
Answer by Notter · Apr 03, 2015 at 01:18 PM
In line 15 you used the array movePositions[i] and, what is movePositions? an array of Transforms! so you get the Transform object
you have to write movePositions[moveToIndex].Position
Brilliant guys, just brilliant. Second time today I've gotten a quality answer from you guys here in the UA.
The working script (for anybody who may need something similar)
using UnityEngine;
using System.Collections;
public class WaypointInvader$$anonymous$$ove : $$anonymous$$onoBehaviour
{
public Transform[] movePositions; // Create array(list) of positions/co-ords
int moveToIndex = 0;
public float speed = 5f;
void Update()
{
if(moveToIndex < movePositions.Length) //if moveToIndex is less than total array items/index do...
{
Vector3 newPos = Vector3.$$anonymous$$oveTowards(transform.position, movePositions[moveToIndex].position, speed * Time.deltaTime);
transform.position = newPos;
if(newPos == movePositions[moveToIndex].position) //if current position = the same as array index do...
{
moveToIndex++; // move to the next array index position
}
}
}
}
Just drop this script onto your gameObject/enemy (whatever you want to move)
Open the inspector, add as many elements(waypoints) as you need.
Create an empty game object, wall it waypoint and drag it into the first slot in the inspector. Now duplicate that object and move it to your second waypoint (do this each time for as many WPs as you have)
Now just press play and your object will move to each WP in order. Basic, but effective waypoint system.
:)
BTW, i wanted to add a little something. look at the error, it says "Assets/WaypointInvader$$anonymous$$ove.cs(15,50)"
the numbers in brackets are the line the error is at, and the character that the error starts from. This will help you pinpoint errors in the future. and of course, read the errors carefully, most of the time they tell you exactly what to do, like in this case.
Answer by jcv8000 · Apr 03, 2015 at 01:28 PM
Change line 15 to
Vector3 newPos = Vector3.MoveTowards(transform.position, movePositions[moveToIndex].position, speed * Time.deltaTime);
Since movePositions is an array of Transforms, "movePositions[moveToIndex]" is a transform, so you have to use ".position" to get the Vector3 position of a transform.
Also Line 18 might need to be changed to
if(newPos == movePositions[moveToIndex].position)
yeah line 18 did throw up an error, but I kind of figured that one.
Thanks for the reply though.
:)
Your answer
Follow this Question
Related Questions
Rotations and waypoints 1 Answer
Transform a Vector by a Quaternion 1 Answer
UI objects move at different speeds on different resolutions 1 Answer
Animation or smooth teleportation 0 Answers