Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Razputin · Jun 08, 2014 at 11:36 PM · c#variablecall

How to call Variable across scripts.

I know theres alot of stuff out there on this but I'm having trouble understanding it. If someone can show me with my scripts I'd appreciate it because I'd understand it in my context and could replicate it later. Being new I really have trouble understanding things when people use their own variables.

How do I get GameHeight and GameWidth from this script

     public GameObject TileOne;
     public GameObject TileTwo;
     public GameObject TileThree;
     public GameObject TileFour;
     public GameObject TileFive;
     public GameObject TileSix;
     public GameObject TileSeven;
     public GameObject TileEight;
     public GameObject TileNine;
     public GameObject TileTen;
     public GameObject TileEleven;
     public GameObject TileTwelve;
     public GameObject TileThirteen;
 
     public int GameWidth;
     public int GameHeight;
     public int Seed;
     public int SeedEntered = 0;
     public int WorldCreated = 0;
 
     public Transform Player; 
 
     void OnGUI() {
 
         if(PlayerPrefs.GetInt("Seed Entered") == 0)
         {
         if(GUI.Button(new Rect(10,400,1200,100), "Create the World"))
         {
                 SeedEntered = 1;
 
         }
         }
         //if(PlayerPrefs.GetInt("World Created") == 0)
         //{
         //if(GUI.Button(new Rect(10,600,1200,100), "Testing Something"))
         //{
         //    Application.LoadLevel("InsideTheInn");
         //    SeedEntered = 1;
         //}
         //}
     }
 
     void Start()
     {
 
         PlayerPrefs.DeleteAll();    //Used for testing new keys, comment out this code when not in use. 
 
         WorldCreated = 0;
 
         if(PlayerPrefs.HasKey("Player Seed"))
         {
             Seed = PlayerPrefs.GetInt("Player Seed");
 
         }
         else
         {
         Seed = Random.Range(0,10000);
         PlayerPrefs.SetInt("Player Seed", Seed);
 
         }    
         if(PlayerPrefs.HasKey ("Seed Entered"))
         {
             SeedEntered = PlayerPrefs.GetInt("Seed Entered");
         }
         else
         {
             SeedEntered = 0;
         }
         if(PlayerPrefs.HasKey("World Created"))
         {
             WorldCreated = PlayerPrefs.GetInt("World Created");
         }
         else
         {
             WorldCreated = 0;
         }
 
     }
 
     void Update () {
         if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
         {
             Random.seed = Seed;
         }
         if(SeedEntered == 1)
         {
             PlayerPrefs.SetInt("Seed Entered", 1);
             if(WorldCreated == 0)
             {
             Random.seed = Seed;
             PlayerPrefs.SetInt("World Created", 1);
             transform.Translate(new Vector3(Random.Range(GameHeight,GameWidth),0.2f,Random.Range(GameHeight,GameWidth)));
             for(int z = 0; z < GameHeight; z++)
                 {
                     for(int x = 0 ; x < GameWidth; x++)
                     {
                         float rnd = Random.value;
                         if(rnd <0.25f)
                         {
                             Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
                         }
                         else if(rnd <0.5f)
                         {
                             Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
                         }
                         else
                         {
                         Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
                         }
                             WorldCreated = 1;
                     }
                 }
             }
         }
      }
 }



And place it in this script

     public Transform player; 
 
 
 
     void Start () {
 
         transform.Translate(new Vector3(Random.Range(GameHeight,GameWidth),0.2f,Random.Range(GameHeight,GameWidth)));
     }
 }
     
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Jopan · Jun 09, 2014 at 12:48 AM

There is multiple ways that you could access those variables from another script. The "easiest" way is to make the variables static.

 public static int GameWidth;
 public static int GameHeight;

There is pros and cons with doing it that way and many would say not to make variables static unless you understand what you are doing. But with those variables marked static you could access them in the other script like this.

 Debug.Log( ClassNameHere.GameWidth );
 Debug.Log( ClassNameHere.GameHeight);

Where "ClassNameHere" is the name of the class that contains the 2 variables. So, the problem with doing it that way, GameWidth and GameHeight will no longer show up in the inspector, so you would have to set them in your script. Another problem is that there would only be 1 GameWidth and 1 GameHeight variable as opposed to 1 for each instance of your class. In the case of "GameWidth" and "GameHeight" that is probably perfectly fine since it makes since to only ever have one. But if you had an "Enemy" script, and each one had a "Health" variable, making them static would not be an option because each enemy needs it's own Health variable.

So the way to do it in most situations would be like this. You would need to declare a variable of type, "ClassNameHere" in your 2nd script.

 public ClassNameHere classInstance;

Then in the inspector the variable should show up on the script component. After that simply drag the object that has your 1st script applied to it onto the classInstance slot on the object that has your 2nd script. Once that is done, you can reference those variables like so.

 Debug.Log( classInstance.GameHeight );
 Debug.Log( classInstance.GameWidth );
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Razputin · Jun 09, 2014 at 01:37 AM 0
Share

Thank you that explained exactly what I needed to know!

avatar image
0

Answer by oasisunknown · Jun 09, 2014 at 12:21 AM

your looking for a get component and its a pretty lengthy explain so I will link you here first.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Components.html

but as an example that you asked for you could do something like.

in another class you would cal

 myHeight = YourObject.GetComponent<ClassName>();

if I am right on that you would store a reference of the class in the new variable you created in a different script myHeight.

you could use it like this for example in the script with myHeight.

 Debug.Log ("Display the game height: ") +myHeight.GameHeight;


another reference I will link you is here to a video that talks about refencing.

Accessing another Class

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Razputin · Jun 09, 2014 at 12:32 AM 0
Share

Is the YourObject the object that the script is attached to in the hierarchy?

 So like... WorldSpawnerComponents = WorldSpawner.GetComponent<$$anonymous$$apCreationScriptV2>();
 

And then when I want to pull the height or the width i'd do like...

    transform.Translate(new Vector3(Random.Range(WorldSpawnerComponents .GameHeight,WorldSpawnerComponents .GameWidth),0.2f,Random.Range(WorldSpawnerComponents .GameHeight,WorldSpawnerComponents .GameWidth)));
avatar image oasisunknown · Jun 09, 2014 at 01:19 AM 0
Share

yes the yourObject is where the script was attached to.

your basically casting the reference into a variable so you can use it for later.

you only need to do the GetComponent once if possible in the start menu then you can use the variable you set it to just like any other variable.

check out the last video that I linked to you in my answer.

and the answer below is very good too.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting index of the smallest number in a list 1 Answer

How big is too big? Terrain Question. 1 Answer

C# Script Template - how to make custom changes? 1 Answer

Help| Convert a javascript to C# 1 Answer

How to transfer colllider from OnTriggerEnter to Update 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges