Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Switch Scenes When "count" Reaches Certain Values

Discussion in 'Scripting' started by TheShadowGamer117, Jan 17, 2018.

  1. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Hello, I'm a beginner and I'm having a problem trying to switch scenes when my value of count reaches a value based on the number of pick-ups the player picks up. I have no problem getting it to work with buttons, but I need it to go to the next level as soon as the number is picked up, without any screen in between. Can someone tell me what I'm doing wrong?

    Here's the player code.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public float speed;
    9.     public Text countText;
    10.     public Text winText;
    11.  
    12.     private Rigidbody rb;
    13.     private int count;
    14.  
    15.     void Start (){
    16.  
    17.         rb = GetComponent<Rigidbody> ();
    18.         count = 0;
    19.         SetCountText ();
    20.         winText.text = "";
    21.     }
    22.  
    23.     void FixedUpdate(){
    24.  
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.  
    28.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    29.  
    30.         rb.AddForce (movement * speed);
    31.     }
    32.      
    33.  
    34.     void OnTriggerEnter(Collider ChangeScene){
    35.  
    36.         if (ChangeScene.gameObject.CompareTag("Pick Up")){
    37.  
    38.             ChangeScene.gameObject.gameObject.SetActive (false);
    39.             count = count + 1;
    40.             SetCountText ();
    41.             if (ChangeScene.gameObject.CompareTag ("Player"))
    42.             {
    43.                 if (count == 1) {
    44.                     SceneManager.LoadScene(2);
    45.                 }
    46.             }
    47.         }
    48.  
    49.     }
    50.  
    51.     void SetCountText (){
    52.  
    53.         countText.text = "Count: " + count.ToString ();
    54.     /*    if (count >= "10"){
    55.  
    56.             winText.text = "You Win!";
    57.         }
    58.         */
    59.     }
    60. }


    Here's the Scene Manager Code


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LoadSceneOnClick : MonoBehaviour {
    7.  
    8.     public void LoadByIndex(int sceneIndex){
    9.      
    10.         SceneManager.LoadScene (sceneIndex);
    11.     }
    12. }
     
  2. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Figured it out if count needs to be removed from OnTriggerEnter and put into "void Update" (which I had to create again)

    Code (CSharp):
    1.     void Update() {
    2.         if (count == 1) {
    3.             SceneManager.LoadScene(2);
    4.         }
    5.     }
    New problem: How do I preserve my score between scenes?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You needn't have moved it to Update(). Your issue was that you were comparing two tags, and the second comparison was nested inside the first. Since each object can have only 1 tag, it would be impossible for that to ever be reached.

    As for preserving data between scenes, there are 3 decent options imo for small data:
    1) playerprefs
    2) a static variable
    3) a game object with DontDestroyOnLoad that has a script that has your score.

    Beyond that, though not asked and I won't make a big deal about it in my post, there are options to serialize (JsonUtility and/or BinaryFormatter). That's good for saving data between sessions, if it's more than preference stuff (and important to not be deleted easily).

    I would suggest to familiarize yourself a little bit with at least the 3 I wrote, and if "between session" data is important, then 1 of the other 2, also. :)

    They can each have their uses, so if you know about them, you'll know about your options.

    Hope that helps. :)
     
  4. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    I'm using Unity for school and we can't use programs/add-on's such as Json, but I'll try give the first three options a shot. Thanks!
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. :) Hope it works for you.

    As a note, though, I'm talking about JsonUtility, which is built into Unity, not an add-on. :)
     
  6. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Okay, so I'm trying to user PlayerPrefs because from what I understand from tutorials and videos about them pretty good, but I can't seem to figure out how to delete the code when I end the game? I'll be using a button by the end, but would like to be able to have the score reset when I do test plays

    I created

    Code (CSharp):
    1.         PlayerPrefs.SetInt("Count", count);
    2.  
    3.         count = PlayerPrefs.GetInt ("Count");
    I would assume that I can use DeleteAll, but where do I put it so that it keeps the prefs between scenes, but not sessions?
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  8. TheShadowGamer117

    TheShadowGamer117

    Joined:
    Jan 17, 2018
    Posts:
    10
    Thanks a ton! Haven't tried resetting on the button yet, adding that tool option made it a lot easier to make sure everything works and so far it's looking all good.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool :)

    I remember being glad when I found that, too. heh.