The question is answered, right answer was accepted
Error BCE0051 with script JS
i am getting the following error Assets/Coin.js(10,10): BCE0051: Operator '+' cannot be used with a left hand side of type 'MenuShopSystem' and a right hand side of type 'int'. I got the variable from the shop script
Coin Script
#pragma strict
var Money : MenuShopSystem ;
function Start () {
}
function OnCollisionEnter ( )
{
Money += 100;
GameObject.Destroy ( gameObject ) ;
}
Shop Script if it helps #pragma strict //add this script to any gameObject you want//
//paste this to add more items to youre shop:
// if(GUI.Button(Rect(Screen.width/2,Screen.height/2,150,150), Item)){
// if(Money >= 200){
// Money -= 200;
// }else{
// Money -=0;
// }
// }
// GUI.Button(Rect(Screen.width/2+150,Screen.height/2,150,150), "Buy: 200");
//variables----------------------------------------
var ShowShop = false;
var CoinTexture : Texture;
public var Money :int=0;
var skin : GUISkin;
var AddButton = false;
//Items:
//to add more items just copy this variable and add the item name;
var Item : Texture;
var Item2 :Texture;
var Item3 :Texture;
//code----------------------------------------
function Start () {}
function Update () {
if(Money <= 0){
Money = 0;
}
}
function OnGUI(){
GUI.skin = skin;
if(ShowShop ==true){
//money{********************-------------------------------------------------------------**************************
GUI.Button(Rect(Screen.width/60,Screen.height/60 ,50,50), CoinTexture);
GUI.Button(Rect(Screen.width/60+50,Screen.height/60 ,70,50), ""+Money);
if(AddButton ==true){
if(GUI.Button(Rect(Screen.width/60+120,Screen.height/60 ,70,50), "Add")){
Money += 100;
}
}
//money}***********************-------------------------------------------------------------------*****************************
//Items(Shop){
if(GUI.Button(Rect(Screen.width/2,Screen.height/2,150,150), Item)){
if(Money >= 200){
Money -= 200;
}else{
Money -=0;
}
}
GUI.Button(Rect(Screen.width/2+150,Screen.height/2,150,150), "Buy: 200");
if(GUI.Button(Rect(Screen.width/2,Screen.height/2- -150,150,150), Item2)){
if(Money >= 150){
Money -= 150;
}else{
Money -=0;
}
}
GUI.Button(Rect(Screen.width/2+150,Screen.height/2- -150,150,150), "Buy: 150");
if(GUI.Button(Rect(Screen.width/2,Screen.height/2- 150,150,150), Item3)){
if(Money >= 500){
Money -= 500;
}else{
Money -=0;
}
}
GUI.Button(Rect(Screen.width/2+150,Screen.height/2- 150,150,150), "Buy: 500");
//Items(Shop)}
}
}//OnGUI End////////////////////////////////////
Answer by Landern · Nov 12, 2015 at 06:30 PM
Money is the name of the variable that is of type MenuShopSystem, you want to modify the value of the field called Money that is in the MenuShopSystem script.
So you would do the object name(in this case you made the variable called Money)
Money
^-- Variable name of type MenuShopSystem
and you want to modify the value of Money which just happens to be called the same as the variable name.
Money.Money += 100;
^---- the field you want to modify that is in the type MenuShopSystem but you're modifying the object you instantiated in coin.js.
Money as the variable name that you're instantiating could be called buttsystem for all the script cares when creating an instance of MenuShopSystem.
Thank you so much :) I appreciate it. I have also rewarded you @Landern
Follow this Question
Related Questions
Help with JS script 0 Answers
Javascript error 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Not compiled 1 Answer
Slicing an int in js/unityscript? 1 Answer