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 /
This question was closed Jan 23, 2016 at 11:09 AM by Syoori for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Syoori · Jul 13, 2013 at 04:11 PM · inputscoreif

If statement not catching certain parameters?

The mechanic I'm trying to work with involves simply recognizing that a block has a certain color and pressing the corresponding button (Shoot vs NoShoot). If you press shoot on a noshoot target, you don't get a score, and vice versa. However, if I press spam the NoShoot button constantly, sometimes it will give the player a point even when the target is a Shoot. Again, this is not happening on every wrong input, and it never happens when pressing the Shoot button repeatedly.

I'm assuming it's a problem with using the Update function to make all this happen, but it may just be poor coding. Any ideas?

  function Update ()
     {    
         if(GameObject.Find("Shoot(Clone)"))
         {        
             if(Input.GetButtonDown("Shoot"))
             {
                 Destroy(GameObject.Find("Shoot(Clone)"));
                  print("GOOD JOB!");
                 score++;
                 Spawn();
             }
             if(Input.GetButtonDown("NoShoot"))
             {
                 Destroy(GameObject.Find("Shoot(Clone)"));
                 print("WRONG");
                 Spawn();
             }        
         }
         
         if(GameObject.Find("NoShoot(Clone)"))
         {    
             if(Input.GetButtonDown("Shoot"))
             {
                 Destroy(GameObject.Find("NoShoot(Clone)"));
                 print("WRONG");
                 Spawn();
             }        
                 
             if(Input.GetButtonDown("NoShoot"))
             {
                 Destroy(GameObject.Find("NoShoot(Clone)"));            
                 print("GOOD JOB!");
                 score++;
                 Spawn();
             }
             
         }
     }
     
 
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Jamora · Jul 13, 2013 at 07:12 PM

Your code doesn't actually check if the mouse cursor is on the gameobjects. The only thing checked is whether or not they exist in the scene, as GameObject.Find() will return null if nothing is found. (Null will always return false when checked in an if.) At some point in your game, you probably have both types of objects in the scene, so the behaviour you described happens.

That beign said, Doing GameObject.Find() repeatedly will kill your framerate, especially if done twice every frame. I suggest you look into using OnMouseDown on your objects that need to be clickable.

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 James U · Jul 13, 2013 at 07:17 PM 0
Share

Expanding on this, you can set a variable equal to GameObject.Find() outside of Update() so that you can use that variable inside of Update() without killing your framerate.

avatar image Jonathon82931 · Jul 13, 2013 at 07:21 PM 0
Share

thanks this is helpful to me

avatar image
1

Answer by James U · Jul 13, 2013 at 06:28 PM

I'm not certain, but I think the problem is with your if statements.

 if(GameObject.Find("Shoot(Clone)"))

This (I think) doesn't make sense because if statements check for true or false values. GameObject.Find() returns a GameObject, and not 1 or 0 (true or false).

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

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

How to change scoreInputField to get a score from another script/Scene 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Trying to stop or change an if 1 Answer

Use Input.GetKeyDown as a function? 1 Answer

Input using the same key twice 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