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 06:52 PM · playervariabletags

Clicking on a Cube. Then -1 a varible

I'm kind of new to Unity3d scripting. And I want to when I click on a Cube that a variable will decrease. Example: wood -= 1

I have set the cubes tag as: Player

So how do I do it? I don't know. I have tried several things. Like Raycast. But I'm not sure if that is used for that. So what do I do?

[EDIT] The script with the "wood" variable is attached to the camera. And I need to decrease the wood variable by clicking on the cube.

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 creighcl · Jul 15, 2013 at 08:18 PM

you need to make sure the "wood" variable is accessible by other scripts. Either its a public variable or you have a public 'setter' method in that script.

Use a raycast to find what was clicked on. If it has the "player" tag, let's subtract the wood from that variable using whatever method you are using. (Script untested, but should get you started)

 If (Input.getMouseButtonUp(0)){
 RaycastHit hit;
 Ray ray = camera.ScreenPointToRay(Input.MousePosition);
 
     
         if (Physics.Raycast(ray, out hit, 100.0f)) 
         {
             //Here's the first object the ray found
             Collider clicked = hit.collider;
             if (clicked.gameObject.tag == "Player"){
               //call your method to subtract wood
               camera.getComponent<Stats>().wood--; //or whatever
             }
         }
 }
Comment
Add comment · Show 3 · 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
avatar image DonDeer · Jul 18, 2013 at 05:48 AM 0
Share

Don't Work. I'm using JavaScript. Is that the reason? It doesn't like:

 RaycastHit hit;
 Ray ray = camera.ScreenPointToRay(Input.$$anonymous$$ousePosition);
 Collider clicked = hit.collider;

avatar image Dothackking · Jul 18, 2013 at 05:50 AM 0
Share

I don't know much about JS, but I think it's hit : RaycastHit, and clicked : Collider

avatar image DonDeer · Jul 18, 2013 at 06:22 AM 0
Share

it didn't accept hit : RaycastHit

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

18 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 avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Assigning varaibles to prefabs 1 Answer

Tagging Trees placed by tree paint 1 Answer

Acces to a var from another Script. 2 Answers

Game Object referencing from another script 0 Answers


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