Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Corey Pullman · Oct 27, 2010 at 03:33 AM · shootingdooropentargets

Shooting target to trigger a door to open

I have not found any answers on here to help me out so I just thought I would ask. Hopefully I am not repeating a question with an answer already, I just haven't found it.

I have a door that needs to be opened after you shoot 3 targets. I have an inventory that keeps track of doors hit, and when you shoot the target it is destroyed and adds 1 to the inventory. I am trying to make it so once the inventory is at 3, the door will open. I already have a working door script so there is no problem on that, so I am just using print("Door is opening!"); to let me know everything is working properly.

//This script is used to open a door when the three targets are hit

function update ()
{
    if(GameManager.targetsHit == 3)
    {
        print("Door Opening!");
    }
}

//Im using print right now just to know if this is working correctly. Right now if I shoot all the targets, it doesn't print.

// This script keeps track of stats, inventory, etc...

static var greenKey: int = 0;
static var targetsHit: int = 0;

//This script destroys the game object when trigger object is hit

function OnTriggerEnter ( other: Collider )
{
    Destroy(gameObject);
    GameManager.targetsHit ++;
    print("Target Hit!");
}

As of right now, the targets get destroyed and the print comes up and says that the target was hit. So it should be adding to the inventory, and the door should open once it reaches 3. . . but it is not. Please help. Thank you in advance!

Comment
Add comment · Show 1
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 · Oct 27, 2010 at 03:38 AM 0
Share

I added code tags so it's easier to read.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by · Oct 27, 2010 at 03:39 AM

Your problem lies with:

function update ()

Code is case-sensitive ... it needs to be:

function Update ()


To be honest, I would recommend considering a change to how you're checking if the number of targets have been hit. Checking every frame until the 'targetsHit == 3' means unnecessary inefficiencies. It will also print the message every frame, and continue to check targetsHit even after it counts past 3.

I would personally suggest checking it just after you increment the count.

function OnTriggerEnter ( other: Collider )
{
    Destroy(gameObject);
    GameManager.targetsHit ++;
    print("Target Hit!");
    if ( GameManager.targetsHit == 3 )
    {
        // open the door here;
    }
}
Comment
Add comment · Show 10 · 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 Corey Pullman · Oct 27, 2010 at 03:54 AM 0
Share

Oh man I didnt even think of that. I changed it, yet it still does not print that the door is opening. Am I doing this the wrong way?

avatar image · Oct 27, 2010 at 04:06 AM 0
Share

If you have it in function Update() and it's not printing, then either targetsHit isn't equal to 3, the script isn't enabled, or the script isn't attached to a GameObject.

avatar image Corey Pullman · Oct 27, 2010 at 04:24 AM 0
Share

I just tried that, and it still doesn't work. BUT in a way it does... cause when I shoot and destroy all the targets, it alerts me that the target is hit at the bottom, and thats all I see. "Target Hit!" BUT when i go into the console alert thing, It says three "Target Hit!" then it says "Door Opening!" then it is followed by three more target hit's...

avatar image Corey Pullman · Oct 27, 2010 at 04:42 AM 0
Share

Well I got it working so that once you shoot all three targets it prints door opening. But there is still a problem, I found out that every time I destroy 1 target, it prints target hit twice, so it is counting each target as 2... so I increased the targetsHit == 6 and it works. No idea why it is counting each as 2 though.

avatar image · Oct 27, 2010 at 04:43 AM 0
Share

I suspect that the same may have been true when you tried it in Update(). Is "Target Hit!" being printed anywhere else in your code? If not, then perhaps you could add "Debug.Log(other.gameObject.name)" to OnTriggerEnter, to see which objects are entering the trigger.

Show more comments

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

No one has followed this question yet.

Related Questions

Opening a door with a key 2 Answers

How to open a gate with a key ? 1 Answer

Trying to open a door so far then stop 1 Answer

Door script lag with rotation (Quaternion.slerp ?) 0 Answers

door script problems 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