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 Tecwyn · Oct 24, 2013 at 10:25 AM · c#collision

Checking collision between 2 objects on 3rd object

Hi,

on my game I have a prefab object called "thebullet". This bullet has a script that when it collides with tag "wall" and "enemy" it gets destroyed.

I have a player object with 2 scripts one for movement and the other shoots the bullet game object. I would like on this script to check to see if the bullet and enemy have collided and if they have increase a score count.

I cannot add the code to the enemy because each enemy will have its own count. I cannot seem to add this script to the bullet because its a prefab object and isn't listed on the hierarchy. So I am left to add it to my "playerShooting" code.

void OnCollisionEnter(Collision collision)

 {
     if(collision.gameObject.tag == "Enemy")
     {
         count = count + 1;
         SetScore();
     } 

This code will not work as its checking to see if the player and the enemy touch. I need to see if GameObject thebullet and the objects tagged as "enemy" have touched. All I know is there is the " FindObjectOfType(typeof(GameObject));" but I have had no luck using that.

Comment
Add comment · Show 4
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 mattssonon · Oct 24, 2013 at 12:10 PM 0
Share

You can add scripts to prefabs fine in the project window, what's the problem?

avatar image Tecwyn · Oct 24, 2013 at 01:19 PM 0
Share

Yes. $$anonymous$$y prefab has a script that checks if it contacts walls and enemys. But when I add a public GUIText I can see it listen on the inspector but I cannot link my "GUItextScore" object to the bullet prefab.

I can see it listened but when I drag and drop it into place it wont work. underneath I can see my public int count and I can set that a value within unity.

$$anonymous$$y university lecture said it might be a problem with the fact that I'm trying to do this with a prefab but he only had a quick look.

avatar image thef1chesser · Oct 24, 2013 at 01:59 PM 0
Share

Isn't the score count something in the world or level class. If you make that public or create a getter and setter for it you can change the score. (if you give the bullet script access to that)

avatar image Tecwyn · Oct 24, 2013 at 02:06 PM 0
Share

thef1chesser that is a good idea. I didn't know I could write script for the level but that does make a lot of sense. I have only been doing unity for 4 weeks at university. This sounds like a good idea if the objects can reference each other. I do not know anything about "getter and setter" but I will look it up.

Thanks for the suggestion!

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

My weapon takes damage. help... 1 Answer

Distribute terrain in zones 3 Answers

animation.isPlaying always true and Animation stuck at time 0 0 Answers

Problem Detecting 2D Collisions 1 Answer

How to move an object over a distance in a direction? 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