Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to change platform sprite after reach some score?

Discussion in 'Scripting' started by Kokomeant, May 18, 2023.

  1. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Hello,
    I'm trying to make a game where you need to climb up and after you passed 100 platform the sprite of platforms will change, so i made a script that spawn that platforms but the main problem is that no matter what i'm trying to do my code turns into long code that hard to read and costs a lot.

    I really will appreciate every help :), even just if you could give me some source that could help me because i really tried to find any information but i didn't found anything that could help me in my case.

    Also, the main reason that I stacked is because I'm using object pooling and I don't know how to change sprite after reach some height or count of platforms.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelManager : MonoBehaviour {
    6.  
    7.     private float targetPlatformHeight = 2.8f;
    8.  
    9.     #region Platforms
    10.  
    11.     private int PlatformsToInit = 10;
    12.  
    13.     private int platformsCounter = 0;
    14.     private int passedScorePlatforms = 0;
    15.     private int targetPlatformsCount = 9;
    16.     private int toNextPlatformCount = 0;
    17.  
    18.     private float distanceToReplacePlatform = 10f;
    19.     private float platformDistance = 2f;
    20.     private float currentPlatformY = 2.8f;
    21.     #endregion
    22.  
    23.     #region Walls
    24.     private int WallsToInit = 3;
    25.  
    26.     private float distanceToReplaceWalls = 15f;
    27.     private float wallDistance = 9f;
    28.     private float currentWallY = 3.5f;
    29.     #endregion
    30.  
    31.     private List<Transform> PlatformsToPoll = new List<Transform>();
    32.     private List<Transform> WallsToPoll = new List<Transform>();
    33.  
    34.     Transform Platform;
    35.     Transform Walls;
    36.  
    37.     private void Awake() {
    38.         Platform = Resources.Load<Transform>("pfPlatform");
    39.         Walls = Resources.Load<Transform>("pfWalls");
    40.  
    41.         InitPlatforms();
    42.         InitWalls();
    43.     }
    44.     private void Start() {
    45.        
    46.     }
    47.     private void Update() {
    48.         SpawnPlatforms();
    49.         SpawnWalls();
    50.         HandleHeightScore();
    51.         Debug.Log($"{platformsCounter}, {passedScorePlatforms}, {toNextPlatformCount}");
    52.     }
    53.  
    54.     private void InitPlatforms() {
    55.  
    56.         for (int i=0; i < PlatformsToInit; i++) {
    57.             if(platformsCounter < targetPlatformsCount) {
    58.                 Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
    59.                 Transform platform = Instantiate(Platform, pos, Quaternion.identity, transform);
    60.                 currentPlatformY += platformDistance;
    61.                 platformsCounter++;
    62.  
    63.                 toNextPlatformCount++;
    64.                 PlatformsToPoll.Add(platform);
    65.  
    66.             } else if (platformsCounter == targetPlatformsCount) {
    67.                 Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
    68.                 Transform platform = Instantiate(Platform, pos, Quaternion.identity, transform);
    69.                 currentPlatformY += platformDistance;
    70.                 platformsCounter = 0;
    71.                 passedScorePlatforms++;
    72.                
    73.                 toNextPlatformCount++;
    74.                 PlatformsToPoll.Add(platform);
    75.             }
    76.         }
    77.     }
    78.     public Sprite[] sprites;
    79.     private int index = 0;
    80.  
    81.     private void SpawnPlatforms() {
    82.          for (int i = 0; i < PlatformsToPoll.Count; i++) {
    83.             if (PlayerController.instance.GetPlayerPosition().y - PlatformsToPoll[i].position.y >= distanceToReplacePlatform) {
    84.                   Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
    85.                   PlatformsToPoll[i].position = pos;
    86.                   PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
    87.  
    88.                   currentPlatformY += platformDistance;
    89.                   platformsCounter++;
    90.  
    91.                   toNextPlatformCount++;
    92.             }
    93.          }
    94.     }
    95.  
    96.     //private void SpawnPlatforms() {
    97.  
    98.     //    for (int i = 0; i < PlatformsToPoll.Count; i++) {
    99.     //        if (PlayerController.instance.GetPlayerPosition().y - PlatformsToPoll[i].position.y >= distanceToReplacePlatform) {
    100.  
    101.     //            if (platformsCounter < targetPlatformsCount) {
    102.     //                Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
    103.     //                PlatformsToPoll[i].position = pos;
    104.     //                PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
    105.  
    106.     //                currentPlatformY += platformDistance;
    107.     //                platformsCounter++;
    108.  
    109.     //                toNextPlatformCount++;
    110.     //            }
    111.     //            else if (platformsCounter == targetPlatformsCount) {
    112.     //                if (passedScorePlatforms < targetPlatformsCount) {
    113.  
    114.     //                    Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
    115.  
    116.     //                    PlatformsToPoll[i].position = pos;
    117.     //                    PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
    118.  
    119.     //                    currentPlatformY += platformDistance;
    120.     //                    platformsCounter = 0;
    121.     //                    passedScorePlatforms++;
    122.  
    123.     //                    toNextPlatformCount++;
    124.  
    125.     //                }
    126.     //                else if (passedScorePlatforms == targetPlatformsCount) {
    127.  
    128.     //                    Vector2 pos = new Vector2(0, currentPlatformY);
    129.  
    130.     //                    PlatformsToPoll[i].position = pos;
    131.     //                    PlatformsToPoll[i].localScale = new Vector3(15f, 3.455f);
    132.  
    133.     //                    currentPlatformY += platformDistance;
    134.     //                    platformsCounter = 0;
    135.     //                    passedScorePlatforms = 0;
    136.  
    137.     //                }
    138.     //            }
    139.     //        }
    140.     //    }
    141.     //}
    142.     private void InitWalls() {
    143.         for (int i = 0; i < WallsToInit; i++) {
    144.             Vector2 pos = new Vector2(0, currentWallY);
    145.             Transform walls = Instantiate(Walls, pos, Quaternion.identity, transform);
    146.             currentWallY += wallDistance;
    147.  
    148.             WallsToPoll.Add(walls);
    149.         }
    150.     }
    151.     private void SpawnWalls() {
    152.         for (int i = 0; i < WallsToPoll.Count; i++) {
    153.             if (PlayerController.instance.GetPlayerPosition().y - WallsToPoll[i].position.y > distanceToReplaceWalls) {
    154.                 Vector2 pos = new Vector2(0, currentWallY);
    155.                 WallsToPoll[i].position = pos;
    156.                 currentWallY += wallDistance;
    157.             }
    158.         }
    159.     }
    160.     private void HandleHeightScore() {
    161.         if (PlayerController.instance.GetPlayerPosition().y >= targetPlatformHeight) {
    162.             Gamemanager.Instance.AddScore(1f);
    163.             targetPlatformHeight += 2f;
    164.         }
    165.     }
    166. }
    167.  
    Thanks in advanced :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    Kokomeant likes this.
  3. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Hey :), first of all sorry for the late response, I debugged all my code and I came to a conclusion that I need to rewrite my script so I completely rewired all my Level Manager script and now it works perfectly. Thank you very much for your advice. :)
     
    Kurt-Dekker likes this.