Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Zoophish · Nov 18, 2015 at 08:36 PM · vector3gameobjectslookatfunctions

Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Vector3' Why is this?

I'm trying to make a turret that shoots a game object with the tag "targetObject", however I keep on getting the same error and thanks to MonoDevelop's very unclear debugging system, I'm not sure how to fix it. (I am new to Unity) Thanks!

pragma strict

 var turretsOn : boolean = true;
 private var targetPosition : Vector3;
 
 function Start () {
     aimStart();
 
 }
 
 function aimStart() {
     
     //Just a loop :)
     while (turretsOn === true) {
         
         //We only want the turret to aim every second, for perfromance and gameplay.
         yield WaitForSeconds(1);
         targetPosition = GetClosestObject("targetObject");
         
         
         Transform.LookAt(Vector3(targetPosition));
         
     }    
     }
 
     
 function GetClosestObject(tag:String) : GameObject {
     
     var objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
     var closestObject : GameObject;
     for (var obj : GameObject in objectsWithTag) {
         if(!closestObject) {
             closestObject = obj;
         }
     //Compares distances
         if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position)) {
             closestObject = obj;
         }
         }
     return closestObject;
     
 
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

1 Reply

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

Answer by NoseKills · Nov 18, 2015 at 09:08 PM

Just like the error is telling you, your function

function GetClosestObject(tag:String) : GameObject

returns a GameObject and you are trying to assign that gameObject into a Vector3 variable

private var targetPosition : Vector3;

If you are trying to store the position of that gameObject, reference it's position vector and store that

targetPosition = GetClosestObject("targetObject").transform.position;

Comment
Add comment · Show 1 · 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 Zoophish · Nov 19, 2015 at 09:10 PM 0
Share

Edit: I fixed this as it was an error by me using an upper case letter for 'transform'.

Thanks for such a quick answer, however I now get "The type 'UnityEngine.Vector3' does not have a visible constructor that matches the arguement list '(UnityEngine.Vector3)'" This is on line 19.

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

39 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 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

Get All Game Objects Between 2 Points 1 Answer

Maintaining look at target between two cameras 1 Answer

Need help making my code more efficient 0 Answers

Need help getting randomly moving particles to head to the nearest of 4 coordinates. 1 Answer

(C#/OOP) Accessing one public function belonging to multiple gameObjects? 2 Answers


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