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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

How To Access Global Variables Across C# Scripts?

Discussion in 'Editor & General Support' started by JeZxLee, Sep 27, 2016.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi,

    We have a global variable called "int ScreenToDisplay" in a C# script called "Initialize"
    and are trying to access the above global variable in another C# script called "main"
    but it's not being found in "main"?

    Assets/scripts/main.cs(34,21): error CS0103: The name `ScreenToDisplay' does not exist in the current context

    Do we need to do something to access global variables across C# scripts?
    Thanks!
     
  2. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi,

    So did a little more research...
    We now have the following 3 C# scripts:

    "Initialize.cs":
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Initialize : MonoBehaviour
    5. {
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         Application.targetFrameRate = 60;
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.    
    17.     }
    18.  
    19. }
    "GlobalVariables.cs":
    Code (CSharp):
    1. public static class GlobalVariables
    2. {
    3.     public const int UnityScreen = 0;
    4.     public const int SixteenBitSoftScreen = 1;
    5.     public const int TitleScreen = 2;
    6.     public const int OptionsScreen = 3;
    7.     public const int HowToPlayScreen = 4;
    8.     public const int HighScoresScreen = 5;
    9.     public const int AboutScreen = 6;
    10.     public static int ScreenToDisplay = SixteenBitSoftScreen;
    11. }
    "main.cs":
    Code (CSharp):
    1.  
    2. /*  JeZxLee's
    3.  _  _  _  _  _  ___  ______  ______  _  _  _______  _  
    4. ( )( )(_)  (_)  (_)  / __)  (_____ \ (______) ( )( )  (_______)  (_)  
    5. |/  \| _  _  ____  _  _| |__  _  _  ____) ) _  _ |/  \|  _____  ____  ____  _  ____  _____ TM
    6.   | |  | ||  _ \ | |(_  __)| | | | / ____/ | |  | |  |  ___)  |  _ \  / _  || ||  _ \ | ___ |
    7.   | |___| || | | || |  | |  | |_| || (_____ | |__/ /  | |_____ | | | |( (_| || || | | || ____|
    8.   \_____/ |_| |_||_|  |_|  \__  ||_______)|_____/  |_______)|_| |_| \___ ||_||_| |_||_____)
    9.   (____/  (_____|  
    10.  
    11.   Cross-Platform Unity Based 2-Dimensional Video Game Engine
    12.  
    13.   (C)opyright 2017 By 16BitSoft Inc.
    14.   www.16BitSoft.com
    15. _____________________________________________________________________________________________________________
    16.  
    17.  
    18. _____________________________________________________________________________________________________________*/
    19. using UnityEngine;
    20. using System.Collections;
    21.  
    22. public class main : MonoBehaviour
    23. {
    24.    // Use this for initialization
    25.    void Start ()
    26.    {
    27.      Initialize();
    28.      
    29.    }
    30.    
    31.    // Update is called once per frame
    32.    void Update ()
    33.    {
    34.  
    35.      if (GlobalVariables.ScreenToDisplay == GlobalVariables.SixteenBitSoftScreen)  DisplaySixteenBitSoftScreen();
    36.  
    37.  
    38.  
    39.    }
    40. }
    We now get the following two errors message in Unity IDE:
    Code (CSharp):
    1. Assets/scripts/main.cs(26,17): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    2.  
    3. Assets/scripts/main.cs(34,95): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    Any idea how to make this work in Unity
     
  3. ErisCaffee

    ErisCaffee

    Joined:
    Nov 26, 2014
    Posts:
    127
    Geez. I tried running your code and it made Unity lock up and almost took the whole Mac down with it. i have no idea what is wrong here.
     
  4. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi, we figured it out: needed "new"...but have a new error :(
     
    Last edited: Sep 27, 2016
  5. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    You should read some tutorials, they will get you started way faster than wondering about things yourself.

    For example, in main.Start() you're calling Initialize script as if it was a method. If you added the "new" before "Initialize", that shouldn't work either, since components need to be attached to GameObjects.

    https://unity3d.com/learn/tutorials/topics/scripting/coding-unity-absolute-beginner
    The tutorials here explain many things, the use of components being the first. Each tutorial there also contains links to related pages, for example the component tutorial has a link to Unity's scripting manual overview. I suggest you start learning the basics from there. I'm sure once you understand the way Unity works on a high level, you'll have no trouble learning the rest.