Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DonDeer · Jul 15, 2013 at 04:21 PM · buildingrtshouse

Making A House

So I'm making a RTS, where you can make houses on locations.alt text

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.

udklip.png (276.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges