Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Variable somehow staying still and not changing numbers.

Discussion in 'Scripting' started by laurexsHV, Apr 12, 2024.

  1. laurexsHV

    laurexsHV

    Joined:
    Mar 23, 2024
    Posts:
    5
    ok so i was just trying to make a game where you push stuff of a platform and i need a variable for obstacle count that is on the main platform(there is 3 platforms, 1st main, 2nd teleportation platform, 3rd platform where all the teleported obstacles go) to make the level end when all the obstacles are pushed off, but when they are pushed off the variable number doesnt move. heres the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class ButtonManager : MonoBehaviour
    9. {
    10.  
    11.     public GameObject StartScreen;
    12.     public GameObject LevelsScreen;
    13.     private ObstacleController obstacleController;
    14.     public TextMeshProUGUI obstacleCountText;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         StartScreen.gameObject.SetActive(true);
    20.         LevelsScreen.gameObject.SetActive(false);
    21.         obstacleCountText.gameObject.SetActive(false);
    22.         obstacleController = GameObject.Find("ObstacleController").GetComponent<ObstacleController>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         obstacleCountUpdater();
    29.     }
    30.  
    31.     // buttons
    32.  
    33.     // Levels button
    34.     public void levelsScreen() {
    35.         StartScreen.gameObject.SetActive(false);
    36.         LevelsScreen.gameObject.SetActive(true);
    37.         obstacleCountText.gameObject.SetActive(false);
    38.     }
    39.  
    40.     public void ExitLevelsScreenButton()
    41.     {
    42.         StartScreen.gameObject.SetActive(true);
    43.         LevelsScreen.gameObject.SetActive(false);
    44.         obstacleCountText.gameObject.SetActive(false);
    45.     }
    46.  
    47.     public void lvl1()
    48.     {
    49.         LevelsScreen.gameObject.SetActive(false);
    50.         obstacleController.obstacleCount = 1;
    51.         obstacleCountText.gameObject.SetActive(true);
    52.         return;
    53.     }
    54.  
    55.     public void obstacleCountUpdater()
    56.     {
    57.         obstacleCountText.text = "Obstacles :" + obstacleController.obstacleCount;
    58.     }
    59. }
    that was for the buttons and heres for the obstacles :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class ObstacleController : MonoBehaviour
    7. {
    8.  
    9.     public float obstacleCount = 60;
    10.     public bool ObsatcleOnPlatform2 = false;
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         obstacleCount = 0;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23.  
    24.     void OnCollisionEnter(Collision other)
    25.     {
    26.         if (other.gameObject.CompareTag("RespawnObstacle"))
    27.         {
    28.             transform.position = new Vector3(0f, -100, 11);
    29.         }
    30.  
    31.         else if (other.gameObject.CompareTag("ObstaclesGround") && ObsatcleOnPlatform2)
    32.         {
    33.             obstacleCount = 3;
    34.             ObsatcleOnPlatform2 = false;
    35.         }
    36.     }
    37. }
    38.  
    some one please help me.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,931
    This code doesn't make any sense. Don't you have multiple obstacles? Why does the ObstacleController keep the count variable? Why would they each have their own count? Why does the ButtonManager script only reference a single ObstacleController?
     
  3. laurexsHV

    laurexsHV

    Joined:
    Mar 23, 2024
    Posts:
    5
    ok so the obstacle count is how much obstacles i have in a map and i will have multiple maps so i set the ibstacle count to thst much obstacles how much is in the current map and when i push off the obstacle the obstacle count should go down by one and when it reaches 0 the game will show you the lvl end screen and the problem is that the obstacle count variable is not going down.
     
  4. laurexsHV

    laurexsHV

    Joined:
    Mar 23, 2024
    Posts:
    5
    and i want it to get set to three for now for tests and in debug log (wich i dont have for now) it say for one frame 1 and for the second it says 3 and then always one but i need it to always say three when the obstacle is pushed off
    please help me fast
     
  5. laurexsHV

    laurexsHV

    Joined:
    Mar 23, 2024
    Posts:
    5
    Oh nvm i fixed it beacause i was creating 2 variables but editing only one and the other staying the same