Basketball Game Crashes

Whenever i run my basketball shooting game on my ipad and iphone it crashes right after the ball is shot and the player resets to a shoot another,

var newball;
static var tempBasketBall :Rigidbody;

var pos :Transform[ ];

var ball2 :Rigidbody;

var canControl2 = true;

var destroyTime :int = 6;

var player2 :GameObject;

var b2Parent :Transform;

var yVel :float;
var zVel :float;

function Start()
{

ball2 = Instantiate (tempBasketBall, pos[1].position, pos[1].rotation);
ball2.transform.parent = b2Parent;

}

function Update()
{
var count = Input.touchCount;

if (count > 0)
{
for (i=0; i<count; i++)
{
var touch : Touch = Input.GetTouch(i);

if (touch.phase == TouchPhase.Began)
{
player2.animation.PlayQueued(“aim”);
}
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
player2.animation.PlayQueued(“fire”);
ball2.transform.parent = null;
ball2.useGravity = true;
ball2.velocity = transform.TransformDirection(0, yVel, zVel);
MakeBall2(pos[1]);
DestroyBall(ball2);
canControl2 = false;
player2.animation.PlayQueued(“idle”);
}
}
}
}

function MakeBall2(pos)
{
yield new WaitForSeconds(1);
ball2 = Instantiate(tempBasketBall, pos.transform.position, pos.transform.rotation);
ball2.transform.parent = b2Parent;
canControl2 = true;
}

function DestroyBall(ball:Rigidbody)
{
yield new WaitForSeconds(destroyTime);
Destroy(ball.gameObject);
}

PLEASE LET ME KNOW WHAT TO DO As I am stuck

Also when I change

function MakeBall2(pos)
{
yield new WaitForSeconds(1);
ball2 = Instantiate(tempBasketBall, pos.transform.position, pos.transform.rotation);
ball2.transform.parent = b2Parent;
canControl2 = true;
}

To
function MakeBall2(pos)
{
yield new WaitForSeconds(5);
ball2 = Instantiate(tempBasketBall, pos.transform.position, pos.transform.rotation);
ball2.transform.parent = b2Parent;
canControl2 = true;
}

The crash occurs after 5sec when the ball is shot

When you destroy the ball you destroy the ball object, so next time you try to instantiate it you get a null reference error and the app crashes.

Also instantiating and destroying object at runtime is very, very bad for performance and memory consumption, it introduces a big overhead and causes garbage collection to trigger often. It’s a lot better to activate and deactivate the same object or a pool of objects. :wink:

Well on the computer I get no errors, which really got to me and I’m new and struggling.

Do you know what I should do? Cause I’m not sure how to activate and deactivate etc

The pooling method would probably be the best.

Search around here on the forums for pooling and you will find a lot of info.

I don’t think it’s about performance because when the ball is destroyed then the game crashes, the ball is supposed to destroy the spawn back in the basketballs hand as it does on the Mac

gameObject.active = true;

and

gameObject.active = false;

To activate/deactivate a gameobject. :slight_smile:

I’m really getting worried that all my hard work going down the drain

I know that, but I’m just not sure how to implement it in my script

Anyone? Please

1st you declared ball as a rigidbody. I would assume it´s a gameobject

2nd try to null out ball after destroying. ball2 = null or whatever it was in unity.

3rd. You usally get crashes when something trys to reverence an object, that doesn´t exist (any more). Do you have some variables or arrays holding ball or some component of it? if so, release that before destroying. Or another script is accessing properties of ball in an update function. Maybe some animation, or something changing the Material?

What the others refer to:
Depending on your situation, it might be better to not destroy the ball, but just to reset it. Position back to player, velocity to zero, and whatever else. Deactivate by ball2.active = false. reactivate ball2.active = true. In beetween you certainly need to change the positon of the ball, but since it has a component of rigidbody, that mostlikly needs to be set to kinematic first. Then change position. Then swith of kinematic. then apply forces again.
This is a little bit more scripting work, but faster. You might also create a “pool” of maximum visible balls. Like 5 to 10. Store those in an array. Instantiate at scene start. Hide all but one. Now, do your throw. First in array gets activate and does its stuff. If the player throws again, while the ball is not done, start ball 2. When the maximum balls are all out, hide/reset the other “done” balls and make them available to throw again.

This way you dont have to instantiate and destroy in game. (Instantiating or in general creating objects is not too fast. With some balls you might not notice too much, but imagine this were Bullets, and you need to create and destroy hundreds, that might be different story.)

Last but not least: Don´t worry, you figured when it crashed, so you will also find the reason why.

And: Check if you have scripts set to slow but safe. This should get you over some problems and maybe output more info when an exception occurs at runtime (check the xcode console).

ok so i ran it in the unity app on my ipad and when i shot it on the computer it says this:

MissingReferenceException: The object of type ‘Rigidbody’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
NewBall+$DestroyBall$10+$.MoveNext () (at Assets/Scripts/NewBall.js:78)

so im understanding you guys but, i have no idea what to replace so it doesnt destroy

please i need to submit this asap please and i really appreciate everyones help, so does anyone know exactly what i need to replace it with?

MissingReferenceException: The object of type ‘Rigidbody’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
NewBall+$DestroyBall$10+$.MoveNext () (at Assets/Scripts/NewBall.js:78)

this bug is making it crash

If you shot only one ball at a time it’s really easy: just call

ball2.active = true;

to activate the ball and:

ball2.active = false;

to deactivate it.
Place it in the scene with correct position and rotation, apply forces and so on. If you have more than one you’ll need an array of object.

Or you can instantiate a prefab. Create a ball prefab, drag and drop the ball prefab in an appropriate variable in your script

var ballprefab : GameObject;

then instantiate it with:

Instantiate (ballprefab, pos[1].position, pos[1].rotation);

You can place the destroy script on the ball prefab: the single object instance in the scene will be destroyed but not the prefab, so you can instantiate another one.

http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html

But activating and deactivating a pool of object without instantiating and destroying is a lot better for performance and memory consumption on mobile platform…

Skyrise can i email you?

I need to ask you something important concerning this problem, as i dont know where, what, and when to activate and deactivate.

Nvm , I managed to fix the crashing problem , THANK YOU ALL SO MUCH! I love this community

Please write something about what you found and how you solved it, so that other users can benefit from that when they happen to read your post because of a similar reason.

I got the same problem in Unity5.
my code like this:

Rigidbody r = bullet.GetComponent();
Bullet b = bullet.GetComponent();

r.velocity = bullet.TransformDirection(Vector3.forward * b.fireForce);

If I use velocity = transform.TransformDirection, the Unity5 will be crash.
If I change to [ r.AddRelativeForce(Vector3.forward * b.fireForce); ] it will be run fine.

Could anyone tell me why?
THanks.