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

Variable Not Assigned Although It Is

Discussion in 'Scripting' started by tomcb, Nov 3, 2018.

  1. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    Hey There
    I have this code that for some reason cant find an asiigned variable (i mean, it does but it doesn't, you will see in a bit)
    this is the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class GameManager : MonoBehaviour {
    9.  
    10.     [HideInInspector] public int levelNumber;
    11.     private GameObject panel;
    12.  
    13.     [Space(10)]
    14.     public GameObject player;
    15.     public GameObject camera;
    16.  
    17.     public GameObject aManager;
    18.  
    19.     private Vector3 playerSpawn;
    20.  
    21.  
    22.     private void Awake()
    23.     {
    24.         panel = GameObject.Find("Panel");
    25.  
    26.         if(panel != null)
    27.             panel.SetActive(false);
    28.  
    29.         player = GameObject.Find("Player");
    30.         camera = GameObject.Find("MainCamera");
    31.         aManager = GameObject.Find("AudioManager");
    32.  
    33.         if (aManager != null)
    34.             Debug.Log("Hello");
    35.  
    36.         levelNumber = SceneManager.GetActiveScene().buildIndex;
    37.         Debug.Log(levelNumber);
    38.  
    39.         if (player != null)
    40.         {
    41.             playerSpawn = player.transform.position;
    42.             player.transform.position = playerSpawn;
    43.         }
    44.     }
    45.  
    46.     public void NextLevel()
    47.     {
    48.         levelNumber++;
    49.         SceneManager.LoadScene(levelNumber);
    50.         DontDestroy();
    51.     }
    52.  
    53.     private void DontDestroy()
    54.     {
    55.         DontDestroyOnLoad(this.gameObject);
    56.     }
    57.  
    58.    
    59.     public void Play()
    60.     {
    61.         if (aManager != null)
    62.             Debug.Log("Ola");
    63.         //aManager.GetComponent<AudioManager>().ChangeTheme();
    64.         SceneManager.LoadScene(1);
    65.     }
    66.  
    67.     public void Options()
    68.     {
    69.         //if (camera.GetComponent<Animator>() != null)
    70.         //{
    71.             Debug.Log("YAY");
    72.             camera.GetComponent<Animator>().SetTrigger("ToOptions");
    73.         //}
    74.     }
    75.  
    76.     public void Quit()
    77.     {
    78.         Application.Quit();
    79.     }
    80.  
    81.     public void Back()
    82.     {
    83.         camera.GetComponent<Animator>().SetTrigger("ToMenu");
    84.     }
    85.  
    86.     public void HowTo()
    87.     {
    88.         //GameObject.Find("Panel").SetActive(true);
    89.         panel.SetActive(true);
    90.         GameObject.Find("BackBtn").GetComponent<Button>().interactable = false;
    91.     }
    92.  
    93.     public void BackToOptipns()
    94.     {
    95.         //GameObject.Find("Panel").gameObject.SetActive(false);
    96.         panel.SetActive(false);
    97.         GameObject.Find("BackBtn").GetComponent<Button>().interactable = true;
    98.     }
    99.  
    100. }
    101.  
    102.  
    i copied only what matters
    the thing is, when i start the game, it does print "Hello" but when I run the Play function it shows the "Variable not Assigned" error.
    the error happenes in the first line in the Play function
    I thouth it might have been somthing with this class:
    Code (CSharp):
    1.  aManager = GameObject.FindGameObjectWithTag("AudioController");
    2.         if (aManager == null)
    3.         {
    4.             Instantiate(audioManager, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0));
    5.             aManager = GameObject.FindGameObjectWithTag("AudioController");
    6.         }
    7.  
    8.  
    9.         aManager.GetComponent<AudioManager>().manager = this.manager;
    (again I copied only what matters)
    but even when i deleted this lines it didn't work
    Any Ideas? Thank you aand sorry for any misspelling
     
    Last edited: Nov 3, 2018
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    firstly, instead of saying
    Code (CSharp):
    1. Instantiate(audioManager, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0));
    you can say
    Code (CSharp):
    1. Instantiate(audioManager, Vector3.zero, Quaternion.identity);
    doesn't really matter, just a shorthand.

    you can also do this(the '(GameObject)' casts it into a GameObject from a C# object)
    Code (CSharp):
    1. aManager = (GameObject)Instantiate(audioManager, Vector3.zero, Quaternion.identity);
    finally, you can store the AudioManager as a variable directly saving you the GetComponent each time(you can still say .gameObject on it if it's a MonoBehaviour)

    now on to the problem at hand, your script is not clear at all, the structure is funky probably from cutting out what "doesn't matter"
    i'd recommend coping all the problematic part even if you don't think it's the problem directly.
     
  3. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    Thank you for your answer. i have edited the post and now you can see the full code
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    try plugging this in for a second:

    Code (CSharp):
    1. public void Play()
    2.     {
    3.         if (aManager == null){
    4.             Debug.Log("aManager is null");
    5.         }
    6.         AudioManager am = aManager.GetComponent<AudioManager>();
    7.         if(am == null){
    8.            Debug.Log("aManager doesn't have AudioManager component");
    9.         }
    10.     }
    11.  
     
  5. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    Same Resault
    it says aManager is null
    but it also prints Hello (line 34)
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    that's weird, where are you calling play from?, maybe something in there is setting it to null.
    or you're calling play before Awake runs on this script
     
  7. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    Play is called way after Awake, since its being called after a button press..