Translating a line to cs

Hello, i’m trying to turn a line of javascript into a line of cs so that it will work with my store gui.
I don’t know enough about cs to translate it myself, thanks everyone!
(the Javascript)

	public var pointCoin : int = pointCoin;

The context I’m using the script in (the cs script) :

#region ADD ITEM TO INVENTORY - called inside OnGUI - "buy" button  
	public var pointCoin : int = pointCoin;  // this allows the coincounter script to show it's inventory and work with the store gui   

	public void addItemToInventory(){           // Method for the buy buttom



        if(selectedItem!= ""){                 //se selectedItem for diferente de null

            print("Bought Item!");

            // To Add Item X amount; - Use a loop   

            

            for(int cnt = 0; cnt < amount; cnt++){

                addItemToInventory(selectedItem);

            }

            

            curpointCoin - totalPrice; // this is why i need the public var so that the term "pointCoin" is recognizable

            

        }

    }

C# variables dont use the term var, and the datatype goes where var would go.

public int pointCoin = pointCoin;

That isn’t true whatsoever. C# allows for type inference, which would be using var, during the build process the implicit-type determined.

examples:

var firstName; // Error: Implicitly-typed variable must be initialized.
var firstName = string.Empty; // this is fine, it knows firstName is a string.

MSDN Documentation

Ahh did not know that.

I replaced the code and it came up with another error “Only assignment, call, increment, decrement, and new object expressions can be used as a statement” on the 24th line of my code:

#region ADD ITEM TO INVENTORY - called inside OnGUI - "buy" button  
	public int pointCoin = pointCoin; // this allows the coincounter script to show it's inventory and work with the store gui   

	public void addItemToInventory(){           // Method for the buy buttom



        if(selectedItem!= ""){                 //se selectedItem for diferente de null

            print("Bought Item!");

            // To Add Item X amount; - Use a loop   

            

            for(int cnt = 0; cnt < amount; cnt++){

                addItemToInventory(selectedItem);

            }

            

            curpointCoin - totalPrice; // this is why i need the public var so that the term "pointCoin" is recognizable// THE ERROR LINE


            

        }

    }

You’re not assigning the value to anything. Try:

curpointCoin -= totalPrice;

or

curpointCoin = curpointCoin - totalPrice;

they both do the same thing.

Here, have a read:

Ok, that worked, but I got a new error message after I added some other references. It turns out that instead of referencing my total amount of money with “pointCoin” I actually made it subtract the cost from the actual gameObject (the coin itself). After swapping “pointCoin” for “coinTotal” (the term designated for my total currency earned) a different error message appeared: The contextual keyword ‘var’ may only appear within a local variable declaration. What I’m trying to do is have my game store script access the total amount of coins, which is information created in my coin counting script, so that when a purchase is made it subtracts the cost from my total. I think that this script will do it but I’m not entirely sure, could you check it to see if I messed up anywhere?

My purchasing section of the store GUI

#region ADD ITEM TO INVENTORY - called inside OnGUI - "buy" button  
 public var coinTotal = int.GetComponent("Coincounter"); // this allows the coincounter script to show it's inventory and work with the store gui   

	public void addItemToInventory(){           // Method for the buy buttom



        if(selectedItem!= ""){                 //se selectedItem for diferente de null

            print("Bought Item!");

            // To Add Item X amount; - Use a loop   

            

            for(int cnt = 0; cnt < amount; cnt++){

                addItemToInventory(selectedItem);

            }

            

        coinTotal -= totalPrice;// this is why i need the public var so that the term "coinTotal" is recognizable

            

        }

    }

The Coin managing Script

static var pointCoin : int = 0;

 var coinTotal:int;
 
//Manger object called "gameManager" script called "GameManager".
 

function Awake () {

    DontDestroyOnLoad (transform.gameObject); //This keeps the manager object from being destroyed.

}
function Start () {

//Manger object called "gameManager" script called "GameManager".




         GameHUDComponent = GetComponent(GameHUD);

 

}

 

function OnGUI() {

 ;

          GUI.Box (Rect (8, 10, 120, 60), "Coins: " + pointCoin);

    

}

What line is the error coming from?

From what I can see, I don’t believe you can use the GetComponent property with an int. If you are trying to return an int from the component CoinCounter, then you should type cast it like (int)GetComponent(“CoinCounter”). I’m not exactly familiar with JavaScript, so the syntax may be a little different, but I seriously doubt that you can use dot notation to type cast the CoinCounter as an int.

Well, the error is coming from line 2 of the store GUI. As for the “int” I thought that the element “coinTotal” was an int (labeled in line 3 of the coin counting script) or do I just need to reference the script, and the component coinTotal will be acknowledged when I use the term in line 24 of the coin counting script? Thanks for checking it by the way.

All slightly confusing … a couple of tips, at the top of each script put comments with the script’s name as it makes it easier to help when there are multiple scripts involved, as below. Fewer empty spaces helps readability too! :wink:

// Coincounter.js

As is you want to do:

public var coinTotal = GetComponent(“Coincounter”).coinTotal;

I can’t test if that works in JS (oh Asset Server, please be quick) but I think it’s what you’re after.

You’ll want to make your Coincounter’s coinTotal var public so it can be accessed, or return it through a public method.

Thank you for the help! I figured out the error, it turns out that the line needs to be within the same brackets that the coinTotal reference is in. It also kicked back two different errors though. The first error was that it didn’t expect the symbol “public” before the line of code. After I removed “public”, it said that UnityEngine.component does not contain a definition for coinTotal, does that mean I need to go back to my Coincounter script to make it recognizable?