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 adrennalinpro · Apr 14, 2014 at 06:40 AM · not workingtargetboxcolliderfindgameobjectwithtag

Shot targets (FindGameObjectWithTag) don't add to "Total Targets Shot" float?

Okay so in order to get this to work I've been adding variable upon variable upon variable... obviously this does nothing unless implemented correctly. The scenario: six targets lined up in a shooting range. If a gameobject with the tag "Bullet" enters one of their BoxColliders, then the script is supposed to add one point (1) to the TargetsShot float, and then send that number to the GUI text. It sends the text alright, that's no problem. BUT: when I shoot a target with the bullet tagged as "Bullet", it does not add 1. In some cases, it adds something like .2, and once all of the targets are shot, it finally ends up being "Targets Shot: 1/6". Something with my math is wrong, and I'm trying to figure out what's happening. Also, it has a hard time detecting all of the bullets at once, so if I shoot a spread of say 6 bullets, it only knows about one of them. BTW, the "1" in "1/6" never goes higher than that.

The animation script for a shot target also deactivates the BoxColliders, preventing glitches after they're shot (like continuing to play the animation by shooting it). I used this fact as a boolean. If the boxcollider is deactivated, then add one point. Pretty simple right? Nope.

These are literal targets that fall over when you shoot them. So why isn't the script doing its job?

Really sorry about the mess.. I was experimenting with different variables. If a variable doesn't seem like it does anything for the script, then just ignore it...

Can anyone think of any alternative to using the Box Collider to do what I want?

My code:

     #pragma strict
     
     var SoundLocation : GameObject;//3D Location of the 3D sound effect
     var Bullet; //The GameObject tagged "Bullet"... not really implemented
     var TargetAnim : String = "TargetShot"; //The animation for the Target.
     var pickup : AudioClip;//Sound after a Target has been shot
     var TargetShot : boolean = false; // If a Bullet with the tag "Bullet" enteres the    //BoxColider, then TargetShot = true.
     var TargetGUI : GUIText; //Area where the text shows up
     var Target : GameObject; //The target itself
     var Targets : float = 6; //How many targets are in the scene
     var AddCount : boolean = false; //Experimental
     var TargetCounter : int = 1; //How many to had to the TargetsDown
     var TargetsDown : float = 0; /How many targets have been shot
     
     function Update () { //made to keep the GUI in one place, weird glitches in past.
     
         TargetGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
     
     }
     
     function OnTriggerEnter(other : Collider) { //If the tag = Bullet...
     
         if(gameObject.GetComponent(BoxCollider).enabled == false) { //If the BoxCol. is deactivated, then add one point to TargetsDown
             TargetsDown += TargetCounter;
         }
         
         if(other.tag == "Bullet") { //If the tag = "Bullet", then initiate animation
             TargetShot = true;
         }
         
         if(TargetShot == true) { //Animation and BoxCollider deactivation
         gameObject.animation.Play(TargetAnim);
         AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position, 8);
         gameObject.GetComponent(BoxCollider).enabled = false;
         }
     }
     
     function OnGUI() {
     
         TargetGUI.text = "Targets Hit: " + TargetsDown + " / " +  Targets.ToString(); 
         TargetGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
     }
 
 EDIT:

Also, when I step inside the collider of a target, it acts like I'M the "Bullet"... not sure what's wrong.

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 HarshadK · Apr 14, 2014 at 07:06 AM

In your OnTriggerEnter you are checking if the BoxCollider is false first (but it is still enabled) and then set it to be false in later if statement at the end.

Actually you should set your BoxCollider to be false first after it is hit and then check for it to increment the TargetsDown variable.

 function OnTriggerEnter(other : Collider) { //If the tag = Bullet...
  
         if(other.tag == "Bullet") { //If the tag = "Bullet", then initiate animation
            TargetShot = true;
         }
  
         if(TargetShot == true) { //Animation and BoxCollider deactivation
         gameObject.animation.Play(TargetAnim);
         AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position, 8);
         gameObject.GetComponent(BoxCollider).enabled = false;
         }

         if(gameObject.GetComponent(BoxCollider).enabled == false) { //If the BoxCol. is deactivated, then add one point to TargetsDown
            TargetsDown += TargetCounter;
         }
     }

The above script check if Game Object is hit then set TargetShot to true, and if TargetShot is true then play animation and set BoxCollider to false, then check if BoxCollider to be false and add TargetCounter to TargetsDown.

One thing though, if you are using integer values for the TargetsDown then you don't need to use flot for it. Whay don't you just use an 'int' instead of 'float'?

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

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

GameObject.FindGameObjectWithTag not finding the tagged objects. 1 Answer

why won't my code work? 1 Answer

Box Collider on Spring Joint Randomly Stops Working in Editor and Builds 1 Answer

Raycast not working correct 0 Answers

How to resort to another object if one is not found? 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