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 venhip · Jul 15, 2012 at 01:45 PM · variablesothercallingfrom

How do you return variables from other scripts?

Ok, I know the first admin to see this will think "This has been asked before! ~delete~" but I just don't understand! The other instances where this has been asked seems to be quite specific to their needs. Rather than saying "This is my code, make it do this" can someone please explain to me, using a random piece of code, how to call a variable from another script? If you do need to see my script I'll put it on but I'd prefer to learn this rather than copy it :) Thanks.

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
3
Best Answer

Answer by whydoidoit · Jul 15, 2012 at 02:25 PM

JS:

   anotherGameObject.GetComponent(ScriptName).myVariable = 1;

   GameObject.Find("someName").GetComponent(ScriptName).DoSomething();

C#:

  anotherGameObject.GetComponent<ScriptName>().myVariable = 1;

  //Find the first instance of the script on the object or any children
  GameObject.Find("someName").GetComponentInChildren<ScriptName>().DoSomething();
Comment
Add comment · Show 12 · 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 Owen-Reynolds · Jul 15, 2012 at 03:35 PM 0
Share

...and look at the Unity manual from http://docs.unity3d.com/Documentation/ScriptReference/index.html. The part above is under "Accessing Other Components."

There are a lot of other good sections that are buried 6 links down from somewhere -- you just have to use a combination of the links in Unity combined with Google and be prepared to bookmark.

avatar image venhip · Jul 15, 2012 at 09:33 PM 0
Share

Ok, I think I get it but I'm returning an error on this:

 if(gameObject.GetComponent<HealthBar>().sta$$anonymous$$a >= 0.0)

Any ideas (i have a script named "HealthBar" with a variable "sta$$anonymous$$a" in it and its attached to the same object as this script) - Thanks for your answers as well!

Edit: error says: "The type or namespace name `HealthBar' could not be found. Are you missing a using directive or an assembly reference?"

avatar image venhip venhip · Jul 15, 2012 at 09:48 PM 0
Share

No, its C#, and I'm sure it is. The variable in the other script is java but ^ this one is in C# because its a modified script from somewhere else. Thanks.

avatar image trickpirata · Jul 15, 2012 at 09:44 PM 1
Share

well if that's in JS it should be

 gameObject.GetComponent(HealthBar).sta$$anonymous$$a

Just make sure you sta$$anonymous$$a variable is accessible.

avatar image trickpirata · Jul 15, 2012 at 09:51 PM 1
Share

In C#:

 gameObject.GetComponent<HealthBar>().sta$$anonymous$$a 

Just make sure that the healthbar is attached with the current gameObject that your workin on.

avatar image whydoidoit · Jul 16, 2012 at 11:36 PM 1
Share

Sorry to be the bearer of bad news. It's a total killer - you really should stick with one language - makes life a lot easier.

Show more comments
avatar image
2

Answer by NothAU · Jul 15, 2012 at 02:23 PM

I'm fairly certain if you declare the variable as "Public Static (Type) (VariableName)", it should be accessible outside of the script by using "ScriptName.VariableName".

But note that declaring it as static means there will only be 1 of that variable, so you don't want to use it for something that multiple objects have, like an enemy's health.

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

9 People are following this question.

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

Related Questions

Calling variables from other scripts 2 Answers

Calling variables in classes from other scripts 2 Answers

how to get a gameObject's name from another scrpit 1 Answer

Issue calling a variable from another script - C# 1 Answer

Change transform variable from outside 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