Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by arinc996 · Sep 13, 2016 at 11:52 AM · c#variables

c# how to use a variable from another script

Hello I have 2 objects one of them is called Player and the other one is Sphere Player has a script in it, that script has an int variable and its set to 1 at start and that int variable called coin when player touches a coin that integer increases by 1

sphere object has diffrent script, I have to print coin integer from this script I tried diffrent codes none of them worked I dont know what to do right now

what should I do?

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

Answer by Alverik · Sep 13, 2016 at 02:32 PM

Well, there are several ways to get scripts or objects to communicate.

1- To get a reference to the object declare a new game object in your script GameObject myObject = new GameObject(); (you'll have to slot the object in the inspector), then you'll have to get a reference to the script you need to access myObject = GetComponent<MyOtherScript>(); then, you'll be able to myObject.MyVarFromOtherScript= //something;.

2- If you are instantiating objects and you wont know the object right away or you need to get a reference to an object automatically then you have to Find the object. There are a many ways to get an object, but to name a few:

myObject = GameObject.FindGameObjectWithTag("myTag"); myObject = FindObjectOfType<MyOtherScript>(); myObject = GameObject.Find("ojbect'sName");

Fastest one is the tag one (you'll have make new tag for it though, but that's great for stuff like "Player"). The one that searches by type of script attached is only going to return the fisrt object it finds with the script, so not that usefull if you have a ton of objects with the script. You can also make the whole process I showed you in the first paragraph in a single line: myObject = GameObject.FindGameObjectWithTag("myTag").GetComponent<MyOtherScript>();

Just bear in mind that finding and object is a slow process so try to do this in Awake() or Start(), never put it on an Update(). Edit: also make sure the variables and methods to call from the other script are public. Else they wont be visible.

3- Another type of communication can be done through global variables, which can be accessed by any script. ie: make a script called GlobalVars, with a static class, then declare your variables as static too (in some cases you may want static readonly for stuff that must NOT be overwritten by other scripts), then you just access the variables using the file name + the variable name (GlobalVars.MyGvar). Static variables exist as a single entity, meaning if you change the value from one script it'll change for all other scripts which are reading it. This is great for settings or gamestates and other data that needs to exist only once.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Declared variable being returned as null in a method? 0 Answers

Problem while using several tags in a OnTriggerEnter (Collider other) 0 Answers

Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos' 1 Answer

Animator Parameter not linking to script variable 0 Answers

Help with converting small pseudo script to C# please? 0 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