Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 zain4 · Aug 08, 2016 at 06:36 PM · scripting problemgameobjectraycastmouse position

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again. here is my code

 void Update() 
 {
  if( Input.GetMouseButtonDown(0) ) { Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
  RaycastHit hit;
      if( Physics.Raycast( ray, out hit, 100 ) )
      {
          if( hit.transform.gameObject.name == "answer" )
              count++;
          print(count);                
      }
      }
  
  }

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
0

Answer by Tibodo · Aug 08, 2016 at 07:54 PM

Yo,

Maybe you can use a Dictionnary (System.Collection.Generic)

 Dictionnary<string, int>

also can store GameObject as id

 Dictionnary<GameObject, int>

But if you can add a component on all wanted gameObjects, you might use the method "OnMouseOver" from MonoBehaviours

 public class Foo : MonoBehaviour
 {
     [SerializeField]
     private int foo = 0;
 
     private void OnMouseOver()
     {
         if(Input.GetMouseDown(0))
         {
             if(foo == 0)
             {
                 foo++;
             }
             else
             {
                 foo--;
             }
         }
     }
 }
Comment
Add comment · Show 2 · 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 zain4 · Aug 09, 2016 at 02:32 PM 0
Share

its is working only for one game object i.e when i click on a gamobject on first click its increment and on second its decrements. what i want to do is if i click on one game object it increment the value of foo=1, then i click on another gameobject it should increment to 2 but it remains 1.

avatar image Tibodo zain4 · Aug 10, 2016 at 12:19 PM 0
Share

Shortest way will be to set foo variable as static, that will be shared between all your GameObjects.

Or to keep your system, use generic collection hashset. It's work by keys, in short, you cannot have the same value in the collection. So in this case, when the raycast hit a GameObject you can add :

private HashSet hashSet = new HashSet();

then after the raycast ;

... if(hashSet.Contains(hit.transform.gameObject)) { count--; hashSet.Remove(hit.transform.gameObject); } else { count++; hashSet.Add(hit.transform.gameObject); } ...

Don't forget add "using System.Collection.Generic"

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to get a VR Gun Raycast to work 1 Answer

I need help with raycasts and with color of gameobject [SOLVED] 3 Answers

Camera switch between child back to main camera issue 2 Answers

One script for many Game Objects with different values 0 Answers

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 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