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 Omer.Hussain · May 01, 2014 at 11:02 AM · raycastonmousedownonmouseenter

How to count numbers on onmousedown?

Hi.. i am stuck at one last point of my game .. in my game i have flower which is made with petals(sprites) and petals are on one another to form a flower in z direction. each petal have 2d collider and my below script. what i am trying to do when user click on petal, petal get disappear and add 1 in the petal counter. when counter is even display its even, and when counter is odd number display its odd. now whats happening it just count once after that counter doesn't count ... it remains on 1 .. and show its odd on every click.. when i comment/remove the gameObject.active=false live it works well.on first click show odd on second show even.. add 1 in every click...Every time i display different number of petals so we click on them untill petals get end.. and at the end either print odd or even ...

 internal var petalStart:int;
 
 function Start(){
 Debug.Log(petalStart);
 
 }
 
 function OnMouseDown()
 
 {
     gameObject.active=false;
     petalStart=petalStart+1;
     print("petal = "+petalStart);
     
     if(petalStart%2==0)
     {
         Debug.Log("its even");
     }
     else{
         Debug.Log("its odd");
         }    
 }

 
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
1
Best Answer

Answer by monogon · May 01, 2014 at 11:21 AM

Like you say you deactivate the gameObject when the user clicks on it, so it cannot receive further events.

Also it seems that you need a static variable if you want this counter to count globally within the class.

 private static var petalStart:int

With your implementation petalStart is unique for each instance, so it never gets greater than 1 because you deactivate the instance on click.

ps.: create a debug.log for this new static variable because you cannot see it in the inspector.

Edit:

There were different problems with your script. First of all you deactivated the gameObject too early, before the variable has been incremented. Secondly gameobject.active is deprecated, use gameObject.SetActive(value: bool) instead like you see in the code below. At last I changed your variable to a static member like I said earlier. (I've just tested this code, it works - just replace it with your code)

 #pragma strict
 
 private static var petalStart:int;
  
 function Start(){
 
 Debug.Log(petalStart);
  
 }
  
 function OnMouseDown()
  
 {
     petalStart++;
     
     print("petal = "+petalStart);
  
     if(petalStart%2==0)
     {
        Debug.Log("its even");
     }
     else{
        Debug.Log("its odd");
        }
        
     gameObject.SetActive(false);
     
     Destroy(gameObject);
 }
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 Omer.Hussain · May 01, 2014 at 01:29 PM 0
Share

but i have, suppose 20 petals and on each petal this script is applied if one deactivate other are still active .. :/ ...i applied such a script in coins collection .. when you collide with coin it add 1 in the score and when you collide with second coin it add 1 again .. coin+=1; ... and coins do get deactivate/destroy.. but whats the matter with onmousedown() function :( ... yes last time i did in the update function for coins... but here when i do in the update function it deactivate all of the petals once rather then it should deactivate only one petal on one click...

 function Update(){
 
 
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
     
     petalStart=petalStart+1;
     print("petals = "+petalStart);
     
     if(petalStart%2==0)
     {
         Debug.Log("its even");
     }
     else{
         Debug.Log("its odd");
         }
 
     }
 }
avatar image monogon · May 01, 2014 at 08:43 PM 0
Share

I edited my answer above. Just replace your code with the provided one. You got me wrong last time. You weren't supposed to completely remove your line where you deactivate the gameObject. It was just at the wrong position. Furthermore you were using petalStart as a dynamic variable where you needed a static one. $$anonymous$$ark this question as solved please if the code works (it works ;) ).

avatar image Omer.Hussain · May 02, 2014 at 11:05 AM 0
Share

Your the $$anonymous$$an ... $$anonymous$$uaaaaaaa :') .. Thanks ..

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

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

Related Questions

A node in a childnode? 1 Answer

How to apply OnMouseDown on sprites according to z position... 3 Answers

RayCasting from camera to player 1 Answer

Raycast Help 1 Answer

Move Towards Target Color 2 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