I'm trying to lerp between two points in my script but i get NullReferenceException
here is the code:
Transform endPos;
float speed;
void Start()
{
endPos.position = new Vector3(0, -1, 1);
}
void Update()
{
transform.position = Vector3.Lerp(transform.position, endPos.position, speed * Time.deltaTime);
}
Interesting thing is that when i set the transform to public and set it in editor it works.
I even tried to use it through creating a gameObject and set the postion through there but no luck.
so how can i do it through script and not editor?
Thanks.
It is happening because your endPos
transform is null when you try to set its position. Use Vector3
instead of transform. Edit your code like this
Vector3 endPos;
float speed;
void Start()
{
endPos = new Vector3(0, -1, 1);
}
void Update()
{
transform.position = Vector3.Lerp(transform.position, endPos, speed * Time.deltaTime);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.