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
0
Question by IndieScapeGames · Mar 15, 2012 at 02:36 AM · variablesclasseslocalglobal

Accessing variables from seperate scripts

I have two quick scripts I whipped up, and what I want to happen, is if a player clicks on a cube as of now, the script attached to the cube will get the int value from the other script and increment the int.

Here are my two scripts:

MouseScript.cs

  using UnityEngine;
     using System.Collections;
     
     public class _MouseScripts : MonoBehaviour {
         public GameObject selCircle;
         public int amountOfIncreaseOfGold = 5;
         
         // Use this for initialization
         void Start () {
     
         }
         
         // Update is called once per frame
         void Update () {
     
         }
         
         void OnMouseDown() {
                //test selection by changing color
             //renderer.material.color = Color.white;
             GameObject clone;
             clone = Instantiate (selCircle, transform.position, transform.rotation) as GameObject;

     _GUI other;
     other.increaseGoldPieces(amountOfIncreaseOfGold);
         }
         
     
     }

And the _GUI script.

_GUI.cs

 using UnityEngine;
 using System.Collections;
 
 public class _GUI : MonoBehaviour {
     //Initializing the different integer variables
     public int goldPieces;
     public int ironOre;
     public int foodStorage;
     public int totalPopulation;
     
     //String displays for on-screen
     private string gold;
     private string iron;
     private string food;
     private string pop;
     
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         gold = goldPieces.ToString();
         iron = ironOre.ToString ();
         food = foodStorage.ToString ();
         pop = totalPopulation.ToString ();
 
     }
     
     void OnGUI() {
         GUI.Label (new Rect (25, 25, 100, 30), "Gold: "+gold);    
         GUI.Label (new Rect (25, 45, 100, 30), "Iron: "+iron);    
         GUI.Label (new Rect (25, 65, 100, 30), "Food: "+food);    
         GUI.Label (new Rect (25, 85, 100, 30), "Population: "+pop);    
     }
     
     public int getGoldPieces() {
         print ("It got here!");
         return goldPieces;        
     }
     
     public void increaseGoldPieces(int n) {
         goldPieces += n;
         getGoldPieces();
     }
 }

I've tried several variations of trying to get this to work, but I get different errors each time. I think I almost had it but I was getting a NULL exception. So I'm hoping you guys can help me out.

Thanks Chad

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

1 Reply

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

Answer by syclamoth · Mar 15, 2012 at 02:43 AM

Both scripts are on the same object, no? If you want to call other.increaseGoldPieces(amountOfIncreaseOfGold), you'll need to actually assign to other first.

What you are actually doing with the line

 _GUI other;

is kind of like making a _GUI-shaped hole where you can stick any _GUI-type object. Without actually putting anything inside that hole, it won't work, because there's nothing there!

Assuming both scripts are on the same gameObject, you can fill that hole using this line (in between creating the reference, and calling increaseGoldPieces)

 other = GetComponent<_GUI>();

GetComponent must be called on either a gameObject, or any component attached to that gameObject. It returns the first found component of the specified type (the bit between the greater-than and less-than signs) that is attached to that object. If no such component exists, it will return null- which can be another source of problems.

Of course, if the GUI script is on a different object, you'll need to somehow get a reference to that object on your cube script. There are a few ways of doing this, including GameObject.Find(string), GameObject.FindWithTag(string tag), keeping a public reference to the GUI script ahead of time using

 public _GUI myGUI;

and assigning it in the inspector before runtime, and other ways (including initialisation scripts etc.). In your case, it's probably simplest to use the public variable/assign in editor method- it's the safest, and isn't difficult to implement.

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 IndieScapeGames · Mar 15, 2012 at 02:54 AM 0
Share

Syclamoth, you covered both bases, and I should have been more specific. The scripts were on different GameObjects, and I followed your instructions exactly and it works!

Thanks for the help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Editing local variables in another script 2 Answers

Global or local variables for use at start function just once 2 Answers

Error when accessing variables from another script: NullReferenceException: Object Reference not set to an instance of an object.. 0 Answers

Class functions don't seem to be updating class variables 1 Answer

rotate 90% in local space 4 Answers


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