• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by GMills · Jan 14, 2011 at 09:27 AM · variabledifferentnon-static

Referencing non static variables from another script? C#

I'm going crazy over here. All I want to do is reference a variable in one script from another. T$$anonymous$$s variable can't be static though. I've gone through a million examples (most in Javascript, and I'm not so good at translating). Not$$anonymous$$ng is working. Here's an example of what I mean:

In a script called "PlayerScript" attached to "ThePlayer" game object:

public class PlayerScript: MonoBehaviour {
float Health = 100.0f;
}

What, very specifically, would I put into another script to get at that Health variable?

Comment
Statement
RDC
Phenoxon
Zeriver
bunnynsnake
someguywithamouse
KodeFromSteam
JanZagar
roeyavraham
BostonGamesLLC
Schleckenmiester
Vibertex
Dev_Parzival
ZachCleaver
Bhanuteja_g
And 8 more...

People who like this

23 Show 1
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 Statement · Jan 14, 2011 at 09:45 AM 1
Share

Notice that the scripting reference has C#, JS and Boo examples. You can toggle which language you want to get help for by selecting from the drop down on the right side near code snippets.

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Statement · Jan 14, 2011 at 09:39 AM

I doubt you've gone through a million examples. See GetComponent docs.

1 - Make Health public.

public class PlayerScript: MonoBehaviour {
    public float Health = 100.0f;
}

2 - Access it.

public class Accessor : MonoBehaviour {
    void Start()
    {
        GameObject thePlayer = GameObject.Find("ThePlayer");
        PlayerScript playerScript = thePlayer.GetComponent<PlayerScript>();
        playerScript.Health -= 10.0f;
    }
}

Basically there are two steps involved.

A. You need a reference to the game object that hold the script, or component that is placed on the same object of your script. T$$anonymous$$s can be found with GameObject.Find, accessing transform c$$anonymous$$ldren, (or just call GetComponent on the running script, see #B).

B. You use GetComponent on either the game object or component, to access your script.

Comment
runevision
bjarnefisker
GMills
Chris12345
Hatredalper
RDC
alexanderameye
Kashkalgar
akursatsahin
Kadaj
luukftw
Phenoxon
Faniha
Abhi94
Joe Long
And 115 more...

People who like this

130 Show 19 · 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 Jesse Anders · Jan 14, 2011 at 04:56 PM 1
Share

@The OP: Note that you don't actually have to make 'Health' public. If you only want to read from it but not write to it, you can expose its value through an accessor or property. (Which you can still do if you want to write to it as well, of course.)

avatar image Statement · Jan 14, 2011 at 08:07 PM 2
Share

But for the sake of learning, and keep progress pace steady, you won't miss out much on just using public fields for now.

avatar image GMills · Jan 16, 2011 at 02:05 AM 3
Share

Thanks Statement! I really did try a whole slew of examples, but I couldn't really find any that spelled it out as well as you did here. I'm pretty new at C# and Unity, so the syntax of this stuff is melting my brain a little bit. I don't fully understand how this works, but I can work toward understanding it now that I've got a working example. Thanks again!

avatar image slipperydemon · Jan 10, 2018 at 02:16 AM 2
Share

How do you use health in the void update?

avatar image SizzlinSam · Oct 28, 2018 at 09:55 PM 2
Share

Statement, so in my game I am trying to make a coin currency system. I have been trying to find out how I could take the variable "coins", which is the amount of coins you collect throughout the level, and add it to the variable "totalCoins"(totalCoins = totalCoins + coins;) ,which is the coins you have collected in other levels. Looking at this sounds super easy, but I would like to then take these variables and display it at my MainMenu scene. I have one C# script that is in the level that counts the coins you collect with a GUI.Label. In my MainMenu scene I have another scene that is supposed to display all of your coins with a different GUI.Label. Do you know what I could do? NOTE: I can add the scripts if needed I guess.

Show more comments
avatar image

Answer by MFKJ · May 14, 2015 at 03:34 PM

Simple no more extra lines

  public class PlayerScript: MonoBehaviour {
         public float Health = 100.0f;
     }

here you can access

 public class Accessor : MonoBehaviour {
     void Start()
     {
         GameObject.Find("ThePlayer").GetComponent<PlayerScript>().Health -= 10.0f;;
     }
 }

Video More: Calling one method from another script

Comment
KasemRaider
W4rf4c3
KodzghlyCZ
Q12x
chesterhilly
ProfBacon
Zarius-Corten
M0N0W0RM
kanoria
Hoorza
LuckyAtmos
CursedLeguna
exafred
bunnynsnake
Hellium
And 23 more...

People who like this

34 Show 7 · 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 Arahan Imon · Jun 02, 2015 at 07:39 AM 0
Share

@MFKJ - Statement gives a cleaner way to do this.

avatar image Q12x · Jun 20, 2016 at 11:17 AM 0
Share

This is the good script that actually works !!!

avatar image Zarius-Corten · Jan 17, 2017 at 06:04 AM 0
Share

This is exactly what I needed, thank you. Simple, effective. Only thing I have to do is make sure it works when I go to use it.

avatar image impurekind · Aug 16, 2018 at 04:52 PM 1
Share

OK, but what about if "Player" is a crate, where there could be a hundred in your level, and you're only trying to affect the health of that specific crate you've shot from inside a raycast check that you first need to do to check if you've even shot the crate in the first place?

I mean, here's what I have for checking the I've shot the crate:

 if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, 500, layerMask))
             {
                 if (hit.collider.gameObject.CompareTag("Crate"))
                 {
                     Debug.Log("Crate Was Hit");
                 }
 }


I don't know how to use the code you've suggested because I don't have a single crate object in the game with a single name that I can easily reference like in the example above (which would of course be fine for on the player). I can't just put "crate" because there's more than one in the level; and I can't obviously write down every single possible crate name I have in my level either (not in any way I know of that wouldn't be crazy). I have a hundred crates and I only want to affect the specific one I'm shooting, and whatever I check and do needs to be done so inside the raycast stuff too (as far as I'm aware). So how would I do that?

avatar image Dragonier135 · Feb 20, 2020 at 10:11 PM 0
Share

This dose not seem to be working for me I just get this error: Assets\weaponSwiching.cs(27,17): error CS0029: Cannot implicitly convert type 'int' to 'bool'

Note: I am a noob I could be doing something completely stupid here

This is my code: GameObject.Find("first person player").GetComponent().Speed = heavyWeightSpeed;;

avatar image ahsen35813 Dragonier135 · Mar 26, 2020 at 11:56 PM 0
Share

Look at GetComponent again. You wrote GetComponent().Speed

You need to mention the script you're actually referencing in there for it to work. It should be like this:

 GetComponent<yourScriptNameSpelledInExactlyTheSameWay>().Speed

so your completed variable should look a bit like this:

 GameObject.Find("first person player").GetComponent<theScriptYourAreReferencing>().Speed = heavyWeightSpeed;

and this will set the variable speed of whatever script you're referencing to heavyWeightSpeed.

Show more comments
avatar image

Answer by rolandcahen · Feb 26, 2012 at 10:51 PM

T$$anonymous$$s point is very important and not done in Unity.

Any real time program should easily allow to share real time variables between any script.

But at the moment is is a real hassle, and never works unless you are a professionnel developer. I have been studying for more than a year and am ashamed I canot ac$$anonymous$$eve such a simple task. Scripting is one t$$anonymous$$ng, beeing a developer is another.

Game designers and non developers users feel excluded from Unity mainly because such functionalities are not simply accessible to them and not clearly explained by examples.

Comment
punzer_ta
the_mr_matt
ios.digitaldividend
Q12x
kamathaj
Xarbrough
bunnynsnake
sean244
Hellium
diliupg
impurekind
charalambousantoniaz
sacredgeometry
ExtraCat
MaxHinskens
And 2 more...

People who like this

11 Show 4 · 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 impurekind · Aug 16, 2018 at 04:26 PM 0
Share

100% agree.

It should be so simple to do this--it is in something like Game Maker Studio for example--but here it seem absurdly convoluted and/or complicated.

And even with the example above, I still can't figure out how to affect only a single instance of whatever object in my game rather than a random one or all of the objects with the same tag. All I want to do is check what I've shot using a raycast--and there could be multiple instances of the same object in the level so I can't just use "name" or "FindGameObjectWithTag" (which is what most examples show)--and then reduce the health of the specific instance of the object I've shot.

avatar image SomeGuy22 · Aug 29, 2018 at 06:14 PM 2
Share

@impurekind you can absolutely affect a single instance of a copied prefab. The Raycast function allows you to pass in a "RaycastHit" variable which contains data on the object that was hit. Let's pretend you named this variable "Hit". All you have to do is check if (hit.transform.GetComponent() != null). Or if you're looking for tags you can check if (hit.transform.gameObject.CompareTag("YourTag")). Then you simply access YourScriptHere again using GetComponent() and reduce it's health. GetComponent() only returns the one instance of the script which is on the object it derives from. In our case, hit.transform.gameObject is the parent, and this object is the one that was hit by the Raycast.

Also, you can share realtime variables between any script. It takes a few lines to set it up, and you have to do it for each script, but it's only a little bit of a hassle in my opinion. The same function (GetComponent) can allow you to access any script on any object. You can store that script as a variable (with a type of YourScriptHere) and then simply edit it's components by writing yourScript.myVariable = 20, etc. The fields on the other script must be public. That's about it, there's no trick. You just have to manually add each script you want to edit at the start of the scene (likely on the Awake() function) and then later access it through the variable you stored it in.

Your other option is static variables, which allow you to read information at any point from any script without an instance--you can access it just by typing YourScriptHere.staticVariable. The downside is that static fields cannot be modified and static functions cannot access non-static information unless it is passed in as an expression.

avatar image ExtraCat SomeGuy22 · Oct 26, 2020 at 11:21 AM 0
Share

It takes a few lines to set it up, and you have to do it for each script != you can share realtime variables between any script. You spend your time figuring out how to pass variable instead of spending it on actual game.

avatar image ntkog · Sep 13, 2022 at 03:51 PM 0
Share

10 years later and sadly, this is still 100% the truth.

avatar image

Answer by Jeremy-Borton · Dec 14, 2021 at 01:45 AM

I don't know if t$$anonymous$$s will help, but I've created a single Damage script that my obstacles, good and bad and player uses. And all it has is a float for crashDamage. That's it. Then in my individual game object main scripts that have crazy names for t$$anonymous$$s bad guy, and that obstacle (having different behaviors), when I have a collision, I just search for the Damage script, get its Damage component and then minus my individual armor from that component's crashDamage.

Using a single script with a common name makes it easy to reference with get component when my main scripts have all kinds of names. My radar picks up the closest object and then I search that object for the common name script and apply its variable to my crazy named objects if need be.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

47 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 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 avatar image

Related Questions

Set variable in script from a different script... 3 Answers

Speed updated from another script, but doesn't persist 1 Answer

Collision object and access to a script variable? 1 Answer

How could you access a script of varying name? 5 Answers

Some script in differents 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