Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Digital-Phantom · Apr 03, 2015 at 01:12 PM · transformvector3convertwaypointcompiler error

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 ?

???

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Apr 03, 2015 at 01:19 PM 2
Share

.position, to be exact.

avatar image Digital-Phantom · Apr 03, 2015 at 01:31 PM 1
Share

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.

:)

avatar image Notter · Apr 04, 2015 at 06:25 PM 0
Share

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.

avatar image
2

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)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Digital-Phantom · Apr 03, 2015 at 01:34 PM 0
Share

yeah line 18 did throw up an error, but I kind of figured that one.

Thanks for the reply though.

:)

avatar image jcv8000 · Apr 03, 2015 at 01:36 PM 0
Share

No problem :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Using Add Force for character control 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges