• 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
Question by bpears · Jan 22, 2013 at 09:56 PM · getcomponent

Comparing variable from another script, help?

Ok, so basically, I am checking the score of the player, and seeing if it is greater or equal to the price. The score number is obtained from another script. Here are the important snipets. The problem is the price is being compared but even though the score is 0, and the price is 100, the rest of the code is still carried out(player still gets weapon). The name of the other script is ScoreManager, hopefully I obtained it's variable currentScore correctly(it is set as static var)?

 var price : int = 100;


 if((!equipped && Input.GetKeyDown ("e"))||ScoreManager.currentScore >= price){
                 Debug.Log("price compared");
                 DropWeapon(weaponToDrop);
                 DeselectWeapon();
   
Comment

People who like this

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

  • Sort: 
avatar image
Best Answer

Answer by cdrandin · Jan 22, 2013 at 10:04 PM

Personally I like keeping things encapsulated within it's own script and for what ever reason if I need to get info from another script I like to make a separate function.

So. Let's say this score manager has private var score. Create a function, getScore() and return the score. It is more typing, yes, but honestly is a better approach for organization so variables aren't directly accessed outside different scripts.

I am not sure what type of gameplay you are trying to get, but make that if statement have an && instead of ||. ALSO, in close score and price with parenthesis. I.e. (ScoreManager.currentScore >= price)

Should be

 if((!equipped && Input.GetKeyDown ("e")) || (ScoreManager.currentScore >= price)){
 
Comment
bpears
Loius

People who like this

2 Show 23 · 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 cdrandin · Jan 22, 2013 at 10:21 PM 1
Share

function getScore (){ return currentScore; }

This should be in your ScoreManager script. Now what ever script this if statement is in call it as such. GetComponent(ScoreManager).getScore(). This way, you have a cleaner method of what is going on and leave the score management up to its own script.

avatar image cdrandin · Jan 22, 2013 at 10:45 PM 1
Share

Dame, I can't remember right now, but I am assuming that your scoreManager isn't attached to anything and I don't know if scripts can be accessed if not attached to an object. Try this. Create empty game object, name it "score manager". Place ScoreManager script onto this game object. Now. Open up the score manager script and type this globally, private var scoreManager : ScoreManager. Now in the Start () function type, scoreManager = GameObject.Find("score manager").GetComponent(ScoreManager) //** this finds that empty gameobject you created which has the scoremanager script, which now gives full access to it. Now, the if statement should be...if((!equipped && Input.GetKeyDown ("e"))&&(scoreManager .getScore() >= price)){

avatar image cdrandin · Jan 22, 2013 at 10:56 PM 1
Share

yeah it will all work. The method I posted above will work. So to reiterate what it does. private var scoreManager : ScoreManager, allocated memory to prepare for the script "ScoreManager". Now in the start we want to able to access the script so we use. scoreManager = GameObject.Find("score manager").GetComponent(ScoreManager), which finds the ScoreManager, game object and then finds the script "ScoreManager", which we now have access to the functions and public/internal variables. Then in the update, we simply call the function via our pointer to the script scoreManager, so scoreManager.getScore(). Simple as that.

avatar image cdrandin · Jan 22, 2013 at 11:05 PM 1
Share

scoreManager = GameObject.Find("ScoreManager").GetComponent(ScoreManager) should be where ever, outside the ScoreManager script. If there are still errors, post both scripts some where so I can just fix it for you.

avatar image cdrandin · Jan 22, 2013 at 11:09 PM 1
Share

To help you along http://www.unity3dstudent.com/2010/07/beginner-b17-tweaking-components-via-script/. Also, check out some of their tutorials to help enforce Unity knowledge. They are simple and helpful. Also, if you are interested on a type of game, check out Unity's already created game tutorial walk thoughts at http://unity3d.com/gallery/demos/demo-projects

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Multiple Cars not working 1 Answer

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

Line Renderer Collision Detection. 2 Answers

My script doesn work Trigger Problem!! HELP!! 3 Answers

I need help with an array 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