- Home /
Problem at crafting
Hi! I tried to make a script that craft a house from the stone and wood that i collect , but when i collect all items needed the button to build dosen't apear . Can someone tell me why? . Thanks !
Collect stone/wood script #pragma strict
private var inventory : Inventory;
function Start () { inventory = GameObject.Find("First Person Controller").GetComponent(Inventory); }
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){ inventory.wood += 1; Destroy (gameObject); } }
house script
pragma strict
var house : GameObject;
private var inventory : Inventory; private var showGUI : boolean = false;
function Start () { inventory = GameObject.Find("First Person Controller").GetComponent(Inventory);
if(inventory.wood >= 2 && inventory.stone >= 1)
GUI.Box(Rect(Screen.width/2 -75,Screen.height/2 - 12.5, 150,25), "Press B to Build");
if(Input.GetKeyDown("b"))
house.SetActive(true);
}
Inventory script
var wood : int = 0; var stone : int = 0;
function OnGUI(){ GUI.Box(Rect(0,0, 100,25),"Wood: " + wood); GUI.Box(Rect(0,25, 100,25),"Stone: " + stone); }
please format your code with selecting it and pressing Ctrl+$$anonymous$$.
Answer by hbalint1 · May 09, 2015 at 03:21 PM
if(inventory.wood >= 2 && inventory.stone >= 1)
GUI.Box(Rect(Screen.width/2 -75,Screen.height/2 - 12.5, 150,25), "Press B to Build");
if(Input.GetKeyDown("b"))
house.SetActive(true);
this part of your code should be in the Update method to cehck it frame by frame. Now you only check the wood and stone count when the game starts, so that's why you can never build.