Checking Null?

Hi,

I have a simple script here attached to a collider and was wondering how do I check if the Object a ball, exists or not?

The ball is an instantiated object which may not exist yet so if it dont exist i’ll get a NullReferenceException So with the script below how would I check if it is null or not there yet.

Im guessing the reason to check if its not there yet is so that I wont get the NullReferenceException or at least thats what Im trying to achieve. Can anyone help me with this?

var power : float = 500.0;
var powerY : float = 50;

function OnTriggerEnter(hit : Collider)
{
	if(hit.gameObject.tag == "ball"  Input.GetKey (KeyCode.Space))
	{
		print("I hit the Ball");
		transform.Find("ball").rigidbody.AddForce(Vector3(0,powerY,power));
	}
}

P.S. Im trying to get it to print(“I hit the Ball”); if its not null or exists inside of the trigger and spacebar has been pressed.

THANK YOU!

Check whether the result of transform.Find is null before doing anything with it.

–Eric

Hi Eric or Anyone :slight_smile:

Thats what I don’t know how to do…(Im not sure how to write it)

So after reading many posts, I tried doing this, and Im not sure if its the correct way?

var power : float = 500.0;
var powerY : float = 50;

function OnTriggerEnter(hit : Collider)
{
	if(hit.gameObject.tag == "ball"  Input.GetKey (KeyCode.Space))
	{
		var myBall = transform.Find("ball");
		
		if(myBall != null)
		{
			print("I hit the Ball");
			myBall.rigidbody.AddForce(Vector3(0,powerY,power));
		}
	}
}

I dont get the error anymore which is a good sign, but when the ball has entered the trigger and I hit the space bar I dont get the print out (I hit the ball) anymore so Im still doing something wrong?

I also tried rearranging it different ways but I cant get the print working so i still gotta be doing something wrong.

Is the ball the only thing with the tag ball? If so…if the if checking the collider hit with the tag ball is true then the ball must exist… right? Then I would just do your print statement as normal, but instead of all the extra checking for the ball…since the tag was already correct… just do a:

hit.transform.rigidbody.AddForce(Vector3(0, powerY, power));

Basically you already determined a ball was there…just give the hit object a kick

Yes, it’s correct.

It can’t really work like that, since OnTriggerEnter is only fired once (when the trigger is entered), and you’re only checking the input that one frame. Although since you’re using GetKey rather than GetKeyDown, it would work if you held the space bar down before OnTriggerEnter fired.

–Eric

Alright, that makes sense I didn’t know the trigger was only fired once.

Yep the ball is the only thing tagged with the name being ball and entering the trigger.

Ok I think I got it, because of your help and this amazing forum :slight_smile:

var power : float = 500.0;
var powerY : float = 50;

function OnTriggerStay(hit : Collider)
{
		
	
	if(hit.gameObject.tag == "ball"  Input.GetKey (KeyCode.Mouse0))
	{
		hit.transform.rigidbody.AddForce(Vector3(0,powerY,power));
	}
}

One huge stumbling block was because I was using iTween to pitch the ball and it was turning on isKinematic. So Im going to have to figure out another way to pitch the ball sad because I had like 10 different paths set up and the ball would randomly pick a path with some really nice curves and slices.

But THANK YOU VERY MUCH! you guys are GREAT! This is working great for hitting the ball :smile:

now its time to figure out a new way to pitch a curve ball… tough one lol.