- Home /
Smooth Follow 2D works but Null Reference Error Remains
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);
}
}
try to use tags and see what happens, this is the syntx: gameObject.FindWithTag("player").transform;
or maybe just don't destroy the ball and change its position. idk just another option.
GameObject.Find... is fine to use, just not the way you are using it (running the find every frame). Place it in start
public var ball:GameObject;
function Start()
{
ball=GameObject.Find("ball(Clone)");
}
or just set the variable from the ball itself. What line is throwing the null??
it is working for him at start, but when he dies or rather destroys the ball and spawns another ball, it isnt working. am i right? @buxton4life
Something like this would be better than constantly running the Find, tho setting target from the ball itself would be best.
if(!target){
target = GameObject.Find("ball(Clone)").transform;
} else {
thisTransform.position.x = $$anonymous$$athf.SmoothDamp( thisTransform.position.x,
target.position.x, velocity.x, smoothTime);
thisTransform.position.y = $$anonymous$$athf.SmoothDamp( thisTransform.position.y,
target.position.y, velocity.y, smoothTime);
}
}
Thanks everyone for the advice.
Yeah @$$anonymous$$ander the follow script runs fine but I just want to get rid of the errors in the log. As the game runs fine in Unity and remote but not Xcode when built.
Absolutely running a find in update was pretty stupid :/
I will try setting the variable from the ball itself, and use that code snippet above it looks much better.
I will report back with the results. Gracias Amigos.
Answer by Caliber Mengsk · Aug 24, 2012 at 07:25 PM
NullReferenceException errors mean that the object you are trying to get does not exist. In this case, the gameobject.find is not finding anything, meaning the variable ball is not set, throwing the null error.
What I would do is set the ball variable through the respawn script, like this: respawnScript:
function OnRespawn()
{
//NOTE: this is just an example function name, use your function, obviously
Camera.main.GetComponent(FollowObjectScript).target = transform;
}
FollowObjectScript: var smoothTime = 0.3;
private var velocity : Vector2;
private var thisTransform : Transform;
public var target : Transform;
function Update()
{
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);
}
thisTransform = transform;
}
This is also much more efficient then running the find command every loop. The find command loops through all game objects in the scene. That means the more objects you have in the scene, the slower it will go, and is a general waste of speed. Sure, it's nice to use some times, but really should not be run every frame. The way I did it above is much faster as it's just setting the reference value, and only does it on the respawn event.
Hope this helps.
Your answer
Follow this Question
Related Questions
iOS Javascript problem 0 Answers
Typed component declaration returning null in iOS 1 Answer
BuildPlayer and Plugins on Ios 1 Answer