- Home /
Making A House
So I'm making a RTS, where you can make houses on locations.
What I'm trying to do is that when I press the green cube. (The cube is one of the locations) A house will pop-up and you will lose resources. Here is my script:
var CurrentMenu;
var house : GameObject;
var wood = 1;
function Start () {
CurrentMenu = "Main";
}
function OnGUI () {
if (CurrentMenu == "Main")
Menu_Main();
}
function NavigateTo (nextmenu)
{
CurrentMenu = nextmenu;
}
function Menu_Main ()
{
GUI.Box(new Rect(5, 5, 100, 25), "Wood: " + wood);
}
function OnMouseDown (house)
{
wood -= 1;
}
I have attached the script to the camera. and the var House is the cube. I could ofc. Attach the "onMouseDown" on the cube. What then the wood, would not go down on the GUI. only in "the cubes wood"
What am I doing wrong? I put the var in () at OnMouseDown.
Answer by Kiloblargh · Jul 15, 2013 at 04:35 PM
function OnMouseDown (house)
This is definitely wrong. OnMouseDown does not accept a GameObject as an argument. (It would be nice if it were that simple, wouldn't it?) You need to have a collider on the green box with a tag and do a raycast from the camera to the mouse position, and if it intersects a collider with that tag, then do something. You can find the exact code to do this several places in the docs and in this site; and since you probably need to do some research on mouse events, colliders, tags, and raycasts anyway- go read up.
var CurrentMenu;
var wood = 1;
This is somewhat wrong- Even if you don't have to declare types of variables, you still always should.
Also, do not start variable names with a capital letter. Use camelCase.
var currentMenu : String;
var wood : int = 1;
if (CurrentMenu == "Main")
Menu_Main();
This is also wrong. Put all your GUI stuff (and nothing but your GUI stuff) in OnGUI(), not in separate functions. Like this:
if (CurrentMenu == "Main")
{ // don't skimp on the curly brackets!
GUI.Box(new Rect(5, 5, 100, 25), "Wood: " + wood);
}
Also, you should not call the place where you click "house". You should call the house prefab "house," and the place where you click on the map "buildableSpot" or something. And you need to check if the wood is greater than 0 before you subtract 1 from it. I know you're not done with the script and are just testing, but you will need to compare the amount of wood you have with the amount it takes to build a house.
Finally, if this is an RTS then it should probably be multiplayer, and if it is multiplayer then you should already be testing NetworkViews and RPC calls and stuff going on to make sure you can sync the positions of multiple units before you go much farther with building houses on the map- and if this is your first game, you should do something else because it's going to be too difficult.
Your answer
Follow this Question
Related Questions
RTS Grid and Pathfinding 2 Answers
Place a building in a RTS game (like Age Of empires 3) 0 Answers
how to add a hole in the wall without effecting texture? 1 Answer
RTS Style building help 2 Answers
Grid system visualization 1 Answer