- Home /
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();
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)){
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.
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)){
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.
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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613091426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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