- Home /
Placing buildings
I would like to have a script, where if you have a certain amount of logs and stones, you can create a campfire. I want a raycast from the middle of the screen to hit a colldier, and instantate a prefab. I then want this prefab to follow the raycast's position, but all my efforts so far have failed. I will post the code for both of these scripts, the GUI and the handler. Builder_GUI.js #pragma strict
private var WindowLoc : Rect = Rect(Screen.width/4, Screen.height/4, Screen.width /2, Screen.height /2);
private var scrollBarChopGrid : Vector2 = Vector2.zero;
private var ShowBuildingMenu : boolean = false;
private var Building_Handler : Building_Handler;
var ToggleBuild : KeyCode = KeyCode.P;
function Start()
{
Building_Handler = GetComponent("Building_Handler");
}
function Update()
{
if (Input.GetKeyDown(ToggleBuild))
if (ShowBuildingMenu)
{
ShowBuildingMenu = false;
}
else
ShowBuildingMenu = true;
}
function OnGUI () {
if (ShowBuildingMenu)
WindowLoc = GUI.Window(0, WindowLoc, Building_Selection, "");
}
function Building_Selection(windowID : int)
{
GUI.Label(Rect(5, 0,100,25), "<b>Item to build:</b>");
GUI.Label(Rect(110, 0,200,25), "<b>Resources required:</b>");
scrollBarChopGrid = GUI.BeginScrollView(Rect (0, 25, Screen.width/2,257), scrollBarChopGrid, Rect(0,0,0,500));
GUI.Label(Rect(5, 0,100,25), "Camp Fire");
GUI.Label(Rect(110,0,200,25), "10xWood, 10xStone");
if (GUI.Button(Rect(315, 0,75,25), "Build"))
{
Building_Handler.Building = true;
Building_Handler.Once = true;
ShowBuildingMenu = false;
}
GUI.EndScrollView();
}
Building_Handler.js
#pragma strict
@HideInInspector
var Building : boolean = false;
var BuildCampFire : GameObject;
@HideInInspector
var Once : boolean = false;
private var hitPointX : int = 0;
private var hitPointY : int = 0;
private var hitPointZ : int = 0;
function Update () {
if (Building)
{
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 50))
{
var clone : GameObject;
hitPointX = hit.point.x;
hitPointY = hit.point.y;
hitPointZ = hit.point.z;
if (Once)
{
clone = Instantiate(BuildCampFire, Vector3(hitPointX,hitPointY + 0.5,hitPointZ), Quaternion.Euler(0,0,0));
Once = false;
}
clone.position = Vector3(hitPointX,hitPointY,hitPointZ);
if (Input.GetMouseButtonDown(0))
{
Building = false;
}
}
}
}
Please reply quickly, so I can get this annoying problem fixed.
Thanks!
Stormy102
I want the instantiated prefab to follow the raycast hit. That should solve everything.
@Scribe nailed your problem. $$anonymous$$ove your 'var clone : GameObject' outside of Update(). As a local variable, it loses its value at the end of each Update() call.
Answer by Scribe · Jul 06, 2014 at 05:22 PM
Clone is being over written every time so it never saves a reference to the instantiated object, move its initialisation to the top of the handler script, outside of the raycast
Your answer
Follow this Question
Related Questions
A way to instantiate an item with 90 Degree increments? 2 Answers
How can I instantiate more prefabs as the score increases? 2 Answers
Prefabs double at each instance? 1 Answer
Grapple Gun? 1 Answer
If Raycast Hits Air 4 Answers