- Home /
Instatiate lerp problem(2D)
I have a score object i want to lerp to the player. This score object instatiates when you kill an enemy. When the enemy is killed the score objects spawns and starts moving but not towards the player but towards the same position everytime (x = 0.0001105822, y = 0.9999889) But when i manually drag out the score object it lerps to the player just fine what could be the issue?
For spawning the objects i use: GameObject scoreIns = Instantiate(scoreObject, transform.position, transform.rotation) as GameObject;
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour
{
// Attached to the score object!
public Transform target;
public float speed = 2.5f;
void Update()
{
transform.position = Vector2.Lerp(transform.position, target.position, speed * Time.deltaTime);
}
}
Where do you set the value of 'target'? You are not getting a null reference exception, so I assume it is getting setup some place.
Hi robertbu. The target is set up in the inspector. I simply drag the player prefab to the public Transform target slot.
Answer by Payt · Jan 02, 2014 at 07:38 AM
Well i fixed the problem. Found out it didnt work if you assign the value of target in the inspector. So i do it in code instead it works!
Thanks for the help anyways! :)