- Home /
Shop system using Enum throws up errors.
This is a simplified part of the script where it doesn't work,
enum Shoptype {R_Rock,R_Iron,R_Silver,R_Gold,R_Gems,R_Uranium,R_Voidium,R_Zarkotium,R_Omnium,ShipFuel,ShipHull,ShipMissiles};
var whichResource : Shoptype;
var ThePlayer : PlayerScript;
function OnMouseDown(){
if(sellbutton == true){
if(sellall == false){
if(ThePlayer.whichResource >= amount){
ThePlayer.whichResource -= amount;
ThePlayer.Money += (price*amount);
guiTexture.color = Color(0.25,0.25,1,1);
}else if(ThePlayer.whichResource < amount){
guiTexture.color = Color(1,0.25,0.25,1);
}
}if(sellall == true){
if(ThePlayer.whichResource >= 1){
theamount = ThePlayer.whichResource;
ThePlayer.Money += (price*theamount);
ThePlayer.whichResource -= theamount;
guiTexture.color = Color(0.25,0.25,1,1);
}else if(ThePlayer.whichResource == 0){
guiTexture.color = Color(1,0.25,0.25,1);
}
}
}else if(sellbutton == false){
if(ThePlayer.Money >= price){
ThePlayer.whichResource += theamount;
ThePlayer.Money -= price;
guiTexture.color = Color(0.25,0.25,1,1);
}else if(ThePlayer.Money < price){
guiTexture.color = Color(1,0.25,0.25,1);
}
}
}
it puts this error at the line,
if(ThePlayer.whichResource >= amount){
"NullReferenceException: Object reference not set to an instance of an object"
not sure what's going on here, I don't work with enums alot.
I don't want to list this as an answer because I am not fluent enough in JS to know for sure, but is it looking for the playerscript in the inspector? So if you have the script assigned to something, look at it in the inspector. There is probably a slot for ThePlayer. Drag your player prefab into that slot. If there is no slot. Then make the top into a public var. so
public var ThePlayer : PlayerScript;
Now it will be looking for your playerscript in the inspector. Drag your player prefab into that slot, and it will get the playerscript off of it. Hopefully that helps. I'm not really sure about JS but it seems its having trouble finding what the ThePlayer is.
Edit* This is assu$$anonymous$$g that the PlayerScript is attached to your player prefab. If it is, then it will know what to do after you drag the player prefab onto the ThePlayer slot in the inspector
I have the line
function Start(){
ThePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent(PlayerScript);
}
In the script, so It finds the player and assigns the player prefab automatically, but its the sell button that doesn't work for some reason.
Strangely, the line, that makes the button a Buy Option works,
}else if(sellbutton == false){
but sell doesn't.
These are the players stats if it helps solve the porblem,
var $$anonymous$$oney : int = 0;
var R_Rock : int = 0;
var R_Iron : int = 0;
var R_Silver : int = 0;
var R_Gold : int = 0;
var R_Gems : int = 0;
var R_Uranium : int = 0;
var R_Voidium : int = 0;
var R_Zarkotium : int = 0;
var R_Omnium : int = 0;
Your answer
![](https://koobas.hobune.stream/wayback/20220613200927im_/https://answers.unity.com/themes/thub/images/avi.jpg)