2
\$\begingroup\$

I have a static public boolean in a JavaScript and I want to access it in a C# script. When I tried the way that you would call a static variable from another script though, it said that the variable and script weren't recognized. Here is my JavaScript code:

public static var ItemPurchased = false;
function OnGUI () {
    if (GUI.Button(new Rect(10, 90, 190, 24), "Click here to collect water.")){
         ItemPurchased = true;
}

I do believe that the JavaScript is correct. On the other hand, here is my C# script:

public static bool itemowned = false;
void Update () {
    if(PurchaseItemScript.ItemPurchased == true){
        itemowned = true;
        PurchaseItemScript.ItemPurchased = false;
    }
}

PLZ HELP!

\$\endgroup\$
1
\$\begingroup\$

Looks like your class is not static. You need to initialize it before using:

public static bool itemowned = false;

private readonly PurchaseItemScript = new PurchaseItemScript();

void Update () {
    if(PurchaseItemScript.ItemPurchased == true){
        itemowned = true;
        PurchaseItemScript.ItemPurchased = false;
    }
}

or, make the PurchaseItemScript class static.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.