• 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
Question by LightSource · Aug 12, 2013 at 02:54 AM · javascripttransformlerp

Unity cannot use Vector3 error = confusion

I am trying to get it so when I reach a barrier, the object bounces back. This is the first time I have really used Lerp and I am a bit confused. Thanks a bunch.

Error: (20,47): It is not possible to invoke an expression of type 'UnityEngine.Vector3'.

 //Original Object
     var object : GameObject;
  
 //Object position + end poition modified
     var objectPos : Vector3;
     var objectEnd : Vector3;
  
 //Controllers
     var duration : float = 1.0;
     var startTime : float;
  
 //Barriers
     var topPos : float = 0;
     var rightPos : float = 5;
     var leftPos : float = 11;
     var bottomPos : float = 16;
  
 function Update () {
  
     if (object.transform.postion.x == rightPos) {
  
        objectEnd.transform.position.x = 4.797853;
  
        object.transform.position = Vector3.Lerp(objectPos, objectEnd(Time.time- 
 startTime) / duration);
 
        }
  
     }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Aug 12, 2013 at 03:51 AM

Your error is due to line 24 which is missing a comma and therefore objectEnd is being consider as an attempt to execute a function call on something that is not a function.

 object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time- 
 startTime) / duration);

Another potential issue is line 20. Having two floats compare to the same values won't happen very often if the one or both of the numbers are calculated. Consider changing it to:

 if (object.transform.postion.x >= rightPos) {
Comment

People who like this

0 Show 7 · 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 LightSource · Aug 12, 2013 at 04:34 PM 0
Share

Thanks a bunch robertbu.

Just for the sake of not making another question, I am getting this:

Object reference not set to an instance of an object

Even though I have set the object in the inspector.

Updated script:

 //Original Object
     var object : GameObject;
 
 //Object position + end poition modified
     var objectPos : Vector3;
     var objectEnd : Vector3;
 
 //Controllers
     var duration : float = 1.0;
     var startTime : float;
 
 //Barriers
     var topPos : float = 0;
     var rightPos : float = 5;
     var leftPos : float = 11;
     var bottomPos : float = 16;
     
     function Start () {
     
     startTime = Time.time;
     objectPos = object.transform.position;
     objectEnd = object.transform.position;
     
     }
 
     function Update () {
  
     if (object.transform.postion.x <= rightPos) {
  
         objectEnd.transform.position.x = 4.797853;
  
         object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
 
         }
 
     }
avatar image robertbu · Aug 12, 2013 at 04:47 PM 0
Share

The typical reason you get an "object not set set" error when you can see the value in the Inspector is that you have the script attached to more than one object, and it is not initialized on the other object. Use the search box in the upper right in the Hierarchy window to search out other game object with this script.

avatar image LightSource · Aug 12, 2013 at 05:19 PM 0
Share

I had considered that and I found that only one object in the scene was assigned the script. When I turn the script off, the error disappears. Also, the script doesn't seem to work. Is it because of the error?

avatar image robertbu · Aug 12, 2013 at 06:20 PM 0
Share

I took a deeper look at your code and found two problems. The first is on line 28. You misspelled 'position'. On line 30, 'objectEnd' is a Vector3, not a GameObject, so you would code it:

 objectEnd.x = 4.797853;

Both issues would have generated an error if '#pragma strict' was at the top of the file.

avatar image robertbu · Aug 12, 2013 at 08:56 PM 1
Share

Sorry, it should be objectEnd.x:

 #pragma strict
 //Original Object
     var object : GameObject;
  
 //Object position + end poition modified
     var objectPos : Vector3;
     var objectEnd : Vector3;
  
 //Controllers
     var duration : float = 1.0;
     var startTime : float;
  
 //Barriers
     var topPos : float = 0;
     var rightPos : float = 5;
     var leftPos : float = 11;
     var bottomPos : float = 16;
  
     function Start () {
  
     startTime = Time.time;
     objectPos = object.transform.position;
     objectEnd = object.transform.position;
  
     }
  
     function Update () {
  
     if (object.transform.position.x <= rightPos) {
  
        objectEnd.x = 4.797853;
  
        object.transform.position = Vector3.Lerp(objectPos, objectEnd, (Time.time - startTime) / duration);
  
        }
  
     }
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Object lerping at start 1 Answer

Detach From Parent 1 Answer

how to launch a ball? 2 Answers

Add specific movement on double click 1 Answer

Move an object to a specific point? 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