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 Mattfro118 · Mar 17, 2014 at 12:01 PM · another scriptc #refer

How to reference a variable from a different script?

So, after doing some Googling this, I came across many Unity-answers pages about this, however I'm not sure if I'm looking to do something different than what I think I am, or If I'm just not comprehending this. I have a 'Vitals' Script. in the script I have two variables, 'curHealth' and 'maxHealth'. I have another script called 'LevelUp'. This script only holds three variables: 'curEXP' and 'maxEXP' and 'PlayerLevel'. I have an 'if' statement setup to add 1 to the PlayerLevel variable. I'd also like to add 10 to the maxHealth variable in the 'Vitals' script. I'm not sure how to go about specifying the variable from the other script. I'm also using C#, so replies using JavaScript may seem foreign. Thanks for your help, ~~Matt

Comment
Add comment · Show 6
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 whydoidoit · Mar 17, 2014 at 12:03 PM 0
Share

Hey $$anonymous$$att - please check what your question looks like and edit if it it's wrong. You had indented your paragraph by 4 spaces which is $$anonymous$$D's way of specifying "format as code". I've fixed it for you.

How are your two scripts related to each other? Are they on the same game object? Do the have a reference? http://unitygems.com/gotchas and http://unitygems.com/getcomponent might help

avatar image semiessessi · Mar 17, 2014 at 01:49 PM 0
Share

Just to make crystal clear: "How are your two scripts related to each other?" is crucial information to being able to answer this question. Without it the question is not well defined enough to have any distinguishable meaning... In general when posing questions like this you should at the least provide enough information that someone can recreate the problem you are having precisely. Without this it is quite literally impossible to answer meaningfully.

avatar image Mattfro118 · Mar 17, 2014 at 01:58 PM 0
Share

Yes the two scripts are attached to the same gameobject. I apologize for not giving enough information, I'm new here, and at Unity, I was unaware this was crucial to the answer. I see now that you can use "GetComponent" when connected to the same gameobject, but what would I do if they were not connected to the same gameobject? And I wanted to have it set up on a collision event? I understand how to set up the collision event, but how would I reference that in my script? Is there a way to call the script?

avatar image semiessessi · Mar 17, 2014 at 06:07 PM 0
Share

Don't worry its a very common problem. Don't blame newness though - this constraint applies to all questions on every subject imaginable. Its just easy to miss out details when you know them all because... well... you know them all and don't need re$$anonymous$$ding like readers of a question do. :)

avatar image semiessessi · Mar 17, 2014 at 06:10 PM 0
Share

what would I do if they were not connected to the same gameobject?

You need some way to locate the other game object. if it is instantiated you need to pass that value around - if it has a name you can use GameObject.Find which is documented: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

And I wanted to have it set up on a collision event?

What does this mean? Set what up?

Is there a way to call the script?

What do you actually mean by script? Its a very ambiguous term in this context. Stick with 'source file' or 'function' for total clarity please.

Show more comments

1 Reply

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

Answer by BobbyDoogle · Mar 17, 2014 at 01:59 PM

Matt, as other members mentioned where your scripts are is important. If the scripts are in the same object do so like this:

(IN SCRIPT 1)

public class SCRIPT1NAME : MonoBehaviour{

public SCRIPT2NAME myScriptReference;

void Start()

{ myscriptReference=GetComponent(SCRIPT2NAME); }

(THEN TO REFERENCE THE VARIABLE OF SCRIPT 2 IN SCRIPT 1)

void myMethod()

{ myScriptReference.SCRIPT2VARIABLE; }

} if the Script two is attached to a different game object you will need one of the following:

GameObject.Find("GAME OBJECT NAME WHERE SCRIPT 2 IS").GetComponent();

or

GameObject.FindGameObjectWithTag("TAG APPLIED TO GAME OBJECT WHERE SCRIPT 2 IS ATTACHED").GetComponent<(SCRIPT 2 NAME)>();

Comment
Add comment · Show 2 · 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 Mattfro118 · Mar 19, 2014 at 05:30 AM 0
Share

Thank you for this response, it really helps to see how it is used in context. This makes alot it easier to visually understand how to do this, Thank you.

avatar image BobbyDoogle · Mar 19, 2014 at 03:42 PM 0
Share

No problem, let me know if you get any errors implementing.

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

24 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Accessing variable in another script? 2 Answers

How to refer to objects, scripts, textures in Assets in C#? 0 Answers

How to C# script a 2d top-down NPC's Animation 0 Answers

Script Error 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