- Home /
How to Place a Game Object in game(Place a building)
Hi i was working on a rts, building type game. I made this script to place the building (a object right now), but it isnt working im not sure whats wrong. var cash : int = 2000; var boxIcon : Texture2D; var sphereIcon : Texture2D; var cylinderIcon : Texture2D; var cubeCost : int = 400; var sphereCost : int = 200; var cylinderCost : int = 600;
private var building;
private var buildingCost : int = 0;
function OnGUI () {
if (GUI.Button (Rect (10,10, 50, 50), boxIcon))
{
building = GameObject.Find("Cube");
buildingCost = cubeCost;
}
if(GUI.Button (Rect (60,10,50,50),sphereIcon))
{
building = GameObject.Find("Sphere");
buildingCost = sphereCost;
}
if(GUI.Button(Rect ( 110,10,50,50),cylinderIcon))
{
building = GameObject.Find("Cylinder");
buildingCost = cylinderCost;
}
}
function Update () {
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit,300)&&building && cash >= buildingCost )
{
Instantiate(building,hit.point,Quaternion.identity );
switch(building)
{
case GameObject.Find("Sphere"):
cash -= 200;
break;
case GameObject.Find("Cube"):
cash -= 400;
break;
case GameObject.Find("Cylinder"):
cash -= 600;
break;
default: print ("nothings the case!");
break;
}
}
else
{
print("not enough money");
}
}
}
What exactly happens? "doesn't work" is a little vague, and your code isn't easy to read. I have a guess though, those gameObject.find are looking for objects that are on the scene already, wouldn't it be more convenient to assign a prefab on the inspector? that way you don't need these objects already in the scene to be able to instantiate new ones.
Your answer
Follow this Question
Related Questions
Gameobject placing halfway through ground? 1 Answer
RTS Grid and Pathfinding 2 Answers
RTS building placement collision problem C# 1 Answer
RTS building snap to grid 2 Answers
Passing through wall 1 Answer