- Home /
multiple instantiate object script help
im making a block based building game and im making it so when you press 1 and left click it places block 1, press 2 block2 etc. so i dont know how to script it. heres my script.
CODE:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
var block1 : Transform;
var block2 : Transform;
var block3 : Transform;
var block4 : Transform;
var whichblock = 1;
function Block4 ()
{
if (Input.GetKeyDown(4))
var whichblock = 4;
}
function Block3 ()
{
if (Input.GetKeyDown(3))
var whichblock = 3;
}
function Block2 ()
{
if (Input.GetKeyDown(2))
var whichblock = 2;
}
function Block1 ()
{
if (Input.GetKeyDown(1))
var whichblock = 1;
}
function Update () {
if (Input.GetMouseButtonDown(0))
{
if (whichblock) == 1;
Build1();
if (whichblock) == 2;
Build2();
if (whichblock) == 3;
Build3();
if (whichblock) == 4;
Build4();
}
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build1() {
if (HitBlock()) {
Instantiate (block1);
block1.transform.position = hit.transform.position + hit.normal;
}
}
function Build2() {
if (HitBlock()) {
Instantiate (block2);
block2.transform.position = hit.transform.position + hit.normal;
}
}
function Build3() {
if (HitBlock()) {
Instantiate (block3);
block3.transform.position = hit.transform.position + hit.normal;
}
}
function Build4() {
if (HitBlock()) {
Instantiate (block4);
block4.transform.position = hit.transform.position + hit.normal;
}
}
function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
Answer by aldonaletto · Feb 11, 2012 at 03:38 PM
The functions Block1 to Block4 were not being called anywhere - but they would not work anyway, because the var keyword declared a new and temporary whichblock variable in each function: temporary variables are deleted on exit, and the original whichblock would never be modified.
I would change the first script part like this:
var blockLayer : LayerMask = 1; var range : float = Mathf.Infinity; var hit : RaycastHit; var block1 : Transform; var block2 : Transform; var block3 : Transform; var block4 : Transform; var whichblock = 1;
function Update () { // check the pressed keys in Update! if (Input.GetKeyDown("1")) whichblock = 1; if (Input.GetKeyDown("2")) whichblock = 2; if (Input.GetKeyDown("3")) whichblock = 3; if (Input.GetKeyDown("4")) whichblock = 4; if (Input.GetMouseButtonDown(0)){ switch (whichblock){ // switch is easier to use case 1: Build1(); break; case 2: Build2(); break; case 3: Build3(); break; case 4: Build4(); break; } } if (Input.GetMouseButtonDown(1)) Erase(); }
// the rest of the script remains the same
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Changing of Variables (Big Noob Question) 1 Answer
Help, How can I get variables? 1 Answer
Instantiate prefab within parameters?(Javascript) 1 Answer
What variables can i declare? 1 Answer