I’m attempting to change the sprites of the balls in play in my brick breaker game. I currently have a regular ball and, if the player gets the powerup, a couple other candy balls, all tagged “ball” that are able to break bricks. They both have the “Ball” script on them. I have an overall manager script that handles when powerups hit the paddle. I’m attempting to code a powerup that, when it hits the collider, it changes the tag of the balls in play from “ball” to “soft ball” (I’m not considering capitalization while writing these tags). That will stop them from being able to break bricks. THIS part of the code works. The problem comes when I try to change the sprite image. I want it to APPEAR as a “soft ball” while the powerup is active, then turn back. Here is the section of the code on the manager script:
public void ActivePopcorn()
{
GameObject[] balls = GameObject.FindGameObjectsWithTag("Ball");
foreach(GameObject sball in balls)
{
sball.transform.tag = "Soft Ball";
ShowNextHitSprite();
}
StartCoroutine(UnactivePopcorn());
}
// Ienumerator UnictiveShield
IEnumerator UnactivePopcorn()
{
// Wait seconds(shieldDuration) to execute next lines
yield return new WaitForSeconds(popcornDuration);
// Get Animator component and set this bool "on" to false and it unactive shield.
GameObject[] balls = GameObject.FindGameObjectsWithTag("Soft Ball");
foreach(GameObject sball in balls)
{
sball.transform.tag = "Ball";
ReturnSprite();
}
}
public void ShowNextHitSprite()
{
Debug.Log("1");
snowBall.ChangeHitSprite();
Debug.Log("2");
}
public void ReturnSprite()
{
Debug.Log("3");
snowBall.ChangeBack();
Debug.Log("4");
}
When playing, the console only Debugs “1” so it doesn’t get very far into the script.
In the Ball script I have:
public void ChangeHitSprite()
{
if(hitSprites[1] != null)
{
GetComponent<SpriteRenderer>().sprite = hitSprites[1];
}
else
{
Debug.Log("Block sprite is missing array" + gameObject.name);
}
}
public void ChangeBack()
{
if(hitSprites[0] != null)
{
GetComponent<SpriteRenderer>().sprite = hitSprites[0];
}
else
{
Debug.Log("Block sprite is missing array" + gameObject.name);
}
}
And even though I’m getting a nullReference, at the " snowBall.ChangeHitSprite();" line from the manager script, the Debug in the ball script isn’t being thrown either. I have sprites set in the ball script component in the ball, and candy ball prefabs, and yet nothing. I can’t figure out what the issue is, any help would be great.