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 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
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
2
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
Add comment · 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 Score$$anonymous$$anager script. Now what ever script this if statement is in call it as such. GetComponent(Score$$anonymous$$anager).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 assu$$anonymous$$g that your score$$anonymous$$anager 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 Score$$anonymous$$anager script onto this game object. Now. Open up the score manager script and type this globally, private var score$$anonymous$$anager : Score$$anonymous$$anager. Now in the Start () function type, score$$anonymous$$anager = GameObject.Find("score manager").GetComponent(Score$$anonymous$$anager) //** 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.Get$$anonymous$$eyDown ("e"))&&(score$$anonymous$$anager .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 score$$anonymous$$anager : Score$$anonymous$$anager, allocated memory to prepare for the script "Score$$anonymous$$anager". Now in the start we want to able to access the script so we use. score$$anonymous$$anager = GameObject.Find("score manager").GetComponent(Score$$anonymous$$anager), which finds the Score$$anonymous$$anager, game object and then finds the script "Score$$anonymous$$anager", 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 score$$anonymous$$anager, so score$$anonymous$$anager.getScore(). Simple as that.

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

score$$anonymous$$anager = GameObject.Find("Score$$anonymous$$anager").GetComponent(Score$$anonymous$$anager) should be where ever, outside the Score$$anonymous$$anager 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

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

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

Player lives script help 1 Answer

Animation spins wildly after completed 0 Answers

hide object start script 4 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