- Home /
null and Null isnt the same?
hello, i am using this script to force an object to follow another game object as soon as it enters its trigger. If the object leaves the trigger and gets out of range the target should be set to "null", as long as no target is set the script prints "null", but if an object is able to get out of the detection range the script prints "Null", because of this the if conditions like if(target==null) dont work since null!=Null. How can i fix this?
public class AIBasic : MonoBehaviour {
public GameObject target;
private Vector3 targetp;
public float speed;
public float followrange;
public SphereCollider triggersphere;
void Start () {
followrange+=triggersphere.radius;
}
void OnTriggerEnter(Collider e)
{
if(target==null)
{
target=e.collider.gameObject;
}
}
void Update () {
print(target);
if(target!=null)
{
targetp=target.transform.position;
Vector3 trans=targetp-transform.position;
float transabs=Mathf.Abs(trans.x)+Mathf.Abs(trans.y)+Mathf.Abs(trans.z);
if(transabs<followrange)
{
transform.Translate(trans*speed*Time.deltaTime);
}
else
{
target=null;
}
}
}
}
Well that is a strange nuance. I will upvote in hopes someone can give you a good answer @S$$anonymous$$mGear
Crazy stuff going on, sometimes the script keeps working...even after the object "got away", after a few tries (enter the sphere and get out of it aggain) the script suddenly stops to operate...
There have been some questions lately that have involved some rather odd if( == || != null) conditions; check the last comment from @robertbu here for example and adapt to your code see if this solves the inconsistency in your game code (this doesn't speak to null v Null, I don't know about that)
http://answers.unity3d.com/questions/694896/detect-tag-with-2d-raycasting.html
Ignore the title, it's the if/null stuff that's important