- Home /
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);
}
}
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) {
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);
}
}
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.
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?
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.
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);
}
}
Your answer
Follow this Question
Related Questions
Object lerping at start 1 Answer
Wrong if command 3 Answers
LookAt Problem 1 Answer
Move an object toward an angle in 2d space 0 Answers
Rotation not going past 180? 2 Answers