My sprite renderer loads the wrong sprite on a new scene
Using Unity 4.6.9f1
I'm making a Brick Breaker game and decided to add a powerup feature that increments the power of the ball by 1 each time you hit a powerup block and adjusts the sprite to reflect that. I've created a sprite for each additional power level up to a total of 3 ( so 3 total sprites) and I've gotten the powerups to work properly in each scene.
This issue that I'm having is that I want to carry over the power and it's corresponding sprite into the new scene/level once you beat the current level. I can get it to either reset the sprite to no power but keep the actual power level OR I can get it to set the sprite to the NEXT power level and keep the current power level. I've posted the code for my ball (which handles the powerups) and my level manager (because I load the sprites in one spot there).
Any help would be greatly appreciated; I've been banging my head against this for 2 days.
Thank you! using UnityEngine; using UnityEditor; using System.Collections;
public class Ball : MonoBehaviour {
public static int ballPower = 1;
public Sprite[] ballSprites;
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;
void Start () {
// I Put this IF here to attempt a fix
if (ballPower > 1){
LoadSprites();
}
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
void Update () {
if(!hasStarted) {
//Lock the ball relative to the paddle
this.transform.position = paddle.transform.position + paddleToBallVector;
//Wait for mouse press to launch
if (Input.GetMouseButtonDown(0)) {
Debug.Log ("Mouse Clicked, launch ball");
hasStarted = true;
this.rigidbody2D.velocity = new Vector2 (2f, 10f);
}
}
}
void OnCollisionEnter2D (Collision2D collision) {
Vector2 tweak = new Vector2 (Random.Range (0.1f, 0.3f), Random.Range (0.1f, 0.3f));
if (hasStarted) {
audio.Play ();
this.rigidbody2D.velocity += tweak;
}
}
public void HandlePowerup(){
int maxPow = ballSprites.Length + 1;
Debug.Log ("BallSprite[] Length: " + ballSprites.Length.ToString ());
if (ballPower < maxPow){
LoadSprites();
ballPower++;
}
Debug.Log ("Powering Ball to: " + ballPower.ToString ());
}
public void LoadSprites(){
int spriteInd = ballPower - 1;
if (ballSprites[spriteInd]) {
this.GetComponent<SpriteRenderer>().sprite = ballSprites[spriteInd];
} else {
Debug.Log ("NO SPRITE HERE!!");
}
}
}
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public int currentLevel;
private Ball ball;
void Start (){
ball = GameObject.FindObjectOfType<Ball>();
}
public void LoadLevel(string name) {
Debug.Log("Level load requested for: " + name);
Brick.breakableCount = 0;
//Ball.ballPower = 1;
Application.LoadLevel (name);
Debug.Log ("Loading level: " + name);
}
public void QuitRequest() {
Debug.Log("Quit game requested");
Application.Quit();
}
public void LoadNextLevel() {
Brick.breakableCount = 0;
Application.LoadLevel (Application.loadedLevel + 1);
Debug.Log ("Sprite Index: " + ball.ballSprites.Length.ToString ());
//ball.LoadSprites();
}
// Is called from class Brick
public void BrickDestroyed () {
if (Brick.breakableCount <= 0) {
LoadNextLevel();
}
}
}
Edit: Title mispelling