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
2
Question by Brian-Brookwell · Sep 15, 2012 at 12:42 AM · global variables

Using Global Variables in Unity

I admit to being more used to JAVA than C#. A student working on a project has hit me with a problem and I can't seem to solve it.

The basic idea is to have Droid run around and gather scrap bits. To make this possible he's created one cs file that's more or less like this one:

public class Scrap : Monobehaviour {
    public int scrapValue;
    ...
    }
He's attached this code to several objects in a unity window.

public class Droid : Monobehaviour {

 public doSomething() {
 GameObject closest = searchClosest();
 
 print (closest.name + "-" + closest.scrapValue;
 }

We're getting an error in that none of the methods we can think of based off the online info seems to work. The name will appear but no value. In fact, if we take closest.name off, we get null.

Any help would be appreciated

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Sep 15, 2012 at 12:49 AM

You need to get the Scrap component from the closest game object. You can use:

 closest.GetComponent<Scrap>().scrapValue
Comment
Add comment · 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
1

Answer by Eric5h5 · Sep 15, 2012 at 12:55 AM

See here: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Comment
Add comment · 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
1

Answer by aldonaletto · Sep 15, 2012 at 01:05 AM

Public variables have global visibility, but since you may have several instances of the same script, the name alone isn't enough to access the variable. There are two ways to access the variable scrapValue:

1- If you want to have only one scrapValue, you can declare it as static:

 public static int scrapValue;

A static variable is equivalent to a global variable in non-OOP programming: the variable is the same for all scripts, even if there are several instances of the script where the variable was declared - actually, the variable is created when the program begins, even before any instance of such script exists, and survives until the program ends.
You can access the static variable above as Scrap.scrapValue (className.variableName).

2- If you may have several independent scrapValue variables, you will need a reference to the particular instance of Scrap that you're interested in, or use a reference to its owner object to get the script reference. If closest is a the reference to the GameObject you want, get Scrap with GetComponent:

 GameObject closest = searchClosest(); // find the object
 Scrap scrapScript = closest.GetComponent< Scrap>(); // get its Scrap script
 print (closest.name + "-" + scrapScript.scrapValue); // access scrapValue


 
Comment
Add comment · 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

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

12 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

Related Questions

Is there a difference between initializing a variable in the Start() function and initializing a variable globally? 4 Answers

Create a persistent GameObject using Singleton 3 Answers

Shader global texture reset on scene load? 0 Answers

Access variable in another object 1 Answer

Help with Making A Variable Global in a Scene 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