Search Unity

if Current Scene = Scene Name

Discussion in 'Scripting' started by SpartanSperos, Dec 29, 2019.

  1. SpartanSperos

    SpartanSperos

    Joined:
    Jul 28, 2013
    Posts:
    9
    Hello. I have tried to copy some code I found on the Unity Docs into my script, but I cannot seem to get it to work. Have been fiddling with it for 30 minutes but still no luck. Lines 16 and 18 are the problem. I have gotten no error messages, but the code no longer works as nothing shows up in the debug menu since I added those two lines. Any help would be appreciated, thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class CharValueStores : MonoBehaviour
    7. {
    8.     public static int P1CharNum;
    9.     public GameObject Cube;
    10.     public GameObject P1Red;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         Scene scene = SceneManager.GetActiveScene();
    16.  
    17.         if (scene.name == "V3 Select Screen")
    18.         {
    19.             P1CharNum = Cube.GetComponent<V3SelectScreen>().P1CharNum;
    20.             P2CharNum = Cube.GetComponent<V3SelectScreen>().P2CharNum;
    21.         }
    22.  
    23.         if (P1CharNum == 1)
    24.         {
    25.             Debug.Log("P1 == 1");
    26.         }
    27.  
    28.         if (P1CharNum == 2)
    29.         {
    30.             P1Red.SetActive(true);
    31.         }
    32.     }
    33. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Line 16 and 18 are a blank line and a {, so those aren't your problem.

    If your debugs aren't printing out, then P1CharNum never equals 1, so you probably need to add a debug outside the if statements to see what the value is. Which could also mean your scene name doesn't match "V3 Select Screen".

    Either way, what are you trying to accomplish? This also seems like a bad use of Update, but I'd have to know more about what you're trying to do.