Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by cully39 · Jan 10, 2017 at 06:20 PM · spritescene-loadingspriterendererpowerup

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make a character selection menu (2d game) ? 2 Answers

Sprite not properly updating color 0 Answers

Problem Changing Sprites with an Array of Sprites 1 Answer

How to load multiple sprites at runtime from List? 1 Answer

Get SpriteRenderer from Sprite? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges