Good morrow people of Unity,
I have a smooth follow script attached to the main camera which finds and follows the instantiated ball. This works fine.
Though between the point of the ball being destroyed and re-spawned the null reference appears. This is because the code is returning NULL to the GameObject.FInd bit. It does not effect the running of the game in unity though when I try to publish in Xcode this error stops it working
My question is how do I get around this. I was thinking maybe attach the script to the ball so that it only runs when in the scene??? But i’m not sure.
Also If there is a method that doesn’t involve gameObject.Find that would be awesome as I am publishing for IOS.
Any guidance would be extraordinary!
Thanks legends.
CODE:
var smoothTime = 0.3;
private var velocity : Vector2;
var thisTransform : Transform;
var target : Transform;
function Update()
{
thisTransform = transform;
target = GameObject.Find("ball(Clone)").transform;
if(!target){
} else if (target) {
thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x,
target.position.x, velocity.x, smoothTime);
thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y,
target.position.y, velocity.y, smoothTime);
}
}