Unable to spawn multiple balls

Hi all,

I am trying to spawn 4 additional balls whenever 50% of the blocks in a level are destroyed. I am able to spawn multiple balls at random locations when 50% of the blocks are destroyed. But the problem is the spawned balls are not moving anywhere unless the current ball hits those spawned balls.

I even tried to add velocity to the spawned balls to make it move instantly when it is spawned but that also didn’t help out. I am also getting a bunch of errors “Null Reference : Object reference not set to an instance of an object.”

I can’t quite figure out as to why the balls are not moving instantly as they are spawned and also why I am getting the above error. I am posting the relevant script codes. Any help would me much appreciated. Thanks !

Level.cs

// Spawn multiple balls when 50 % of the blocks in a level are destroyed
private double percentageFactorForBallSpawn = 0.50;
private int percentageValueForBallSpawn = 0;

image

Game Status.cs

image

Ball.cs (The lines where I am getting the error are 27 and 54)

Hi!

Let’s talk about Rigidbodies first. Rigibodies won’t move until you apply a force to them or they are capable to react to gravity, which, in this case, if you are following the course, the gravity setting has been set to 0, so the balls won’t move until some force is applied to them.

Now, onto why they are not moving even after modifying the velocity. Check your SpawnMultipleBalls method, you are not applying force to the instantiated ball but to the prefab. To solve this you have to apply the force to the instantiated ball:

// Store the instantiated game object in a variable to access it's components
GameObject newBall = Instantiate(ballPrefab, randomSpawnPos, ball.transform.rotation);
newball.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 15f);

.

The Null Reference errors.
If you are following the course, you have to set the value of the paddle variable in the editor, that means your new instantiated balls won’t have a value assigned to them, you have to set the value in code to prevent this errors. Add this to the Ball script:

void Start()
{
    // Add this line at the top of your Start method.
    paddle = FindObjectOfType<Paddle>();
    ... 
}

Hope this helps.

1 Like

Hi,

Thanks for helping me out. It was a silly mistake on my part to not assign the velocity of the rigid body to the newly instantiated ball game object. It is working perfectly now and also the nulll reference exceptions are resolved.

However I noticed one more issue. In the ball prefab I observed that we already have the ball sounds as an array which contains the audio clips to play whenever the ball hits a wall or destroys a block. It works fine when there is a single ball.

However, when multiple balls are instantiated the sounds when the new spawned balls hit the walls are not being played. Since they are also being instantiated from the prefab and the prefab already has an array of audio clips, shouldn’t the new balls also automatically have the audio clips ? or am i missing something stupid?

In this case since the balls are being instantiated through code I couldn’t also set the array of audio clips to the newly instantiated balls. Any help is much appreciated. Thanks :slight_smile:

Relevant scripts

// Script where multiple balls are being instantiated
GameStatus.cs

Ball.cs

//Part where audio clips are being played at random from an array whenever the ball collides with walls

Hi @Prashanth_Vamanan,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Are there any error messages in your console when the new balls are supposed to play a sound? Maybe the reference to an AudioSource object is missing in their respective Ball instance.

Hope this helps :slight_smile:


See also;

2 Likes

Hi Nina,

Sorry about sending screenshots. I will ensure that from next time I copy paste and send the proper formatted code so that people can help me out more easily.

Regarding the error messages, I am not seeing any error messages in the console for the new balls being spawned. These balls are being spawned from a prefab and I also checked the ball clones being spawned in the inspector and all of them have an audio source as well as the sounds array with the random sounds.

Cannot quite figure out as to why the sounds are not playing. Thanks : )

Reference : Screenshot of the inspector when multiple balls are being spawned

As you could see even the ball clones have the ball sounds array with the audio clips as well as the audio source attached to them.

I think I got the issue, not quite sure, but it’s probably because the new balls never set the hasGameStarted variable as true, so the sound won’t play, check with a Debug.Log if that if statement in your CollisionEnter method is running.

If that’s the issue you’ll need to find a way to set that variable to true after you instantiate the new balls.

1 Like

Hi,

Thanks for the help. Yes you were right, hasGameStarted was false for newly instantiated balls and I managed to set it to true. Now everything is working fine. Thanks.

I would really recommend myself to spend more time debugging these issues, since I know I should have identified this bug myself. :smile:. Anyways , thanks for your support!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms