How to pass a float value from another script?
I've been working on a simple clicker game and I want to make an upgrade button that is not interactable if a float value of the price in the button is greater than the float value of gold you have. Here are the important bits of my code:
The first script has the float value of gold
public float gold = 0.00f;
The second one has a public float of the upgrade cost that is later assigned a value. It's supposed to compare the cost float in script #2 to the gold float in script #1 in an if statement and I have no idea how to link the float value from script #1.
public float cost;
void Update(){
if (cost > gold)
buyitem.interactable= false;
else
buyitem.interactable = true;
Thanks in advance and sorry if what I've written is unclear.
Answer by BlockFade · Feb 09, 2017 at 06:30 PM
Create a variable for Script 1 in Script 2
public Script1 script1;
//Assign variable in inspector.
script1.gold
// Get Gold variable
Thank you for replying! I tried your solution but I think im doing something wrong. For reference script1 is named ClickGold.cs
In script 2 i wrote:
public ClickGold script1;
and the ClickGold part is red and I'm getting an error CS0246 missing type or namespace
@Sigrik Is the ClickGold script public or in a different folder than script2?
I moved them to the same folder and the error is gone! Thank you so much!