Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mattkell462 · Feb 18, 2018 at 02:11 PM · gameobjectcolliderfunctionscalling

Im having trouble figuring out a way to make these two functions work.

I'm trying to create an apple projectile that will disappear if it hits a certain object. I understand that I am declaring apple as a gameobject and then trying to make it equal to a collider. I just need to know a way to make this logic work. Below are the two functions in question.

 void Fire()
     {
         var apple = (GameObject)Instantiate (applePrefab, appleSpawn.position, appleSpawn.rotation);
         apple.GetComponent<Rigidbody> ().velocity = apple.transform.forward * 10;
 
         Destroy (apple, 5.0f);
 
         if (apple.gameObject.CompareTag("Enemy"))
         {
             FireCollision (apple);
         }
     }
 
     void FireCollision(Collider other)
     {
         other.gameObject.SetActive(false);
         health = health - 5;
     }
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 ShadyProductions · Feb 18, 2018 at 02:23 PM

I don't think you understand how collision is handled in Unity. Each monobehaviour has several methods that unity tries to call through reflection. One of these methods is: OnCollisionEnter(Collision collision)

These methods are called as soon as the monobehaviour collides with an object automatically. You should create a script with following and put this on the applePrefab.

 // Can be on the appleprefab aswel, as soon as the prefab  enters the game it will destroy after 5
 void Start() 
 {
      Destroy (gameObject, 5.0f);
 }

 // A script on the apple prefab should contain this method
 void OnCollisionEnter(Collision collision)
 {
      if (collision.CompareTag("Enemy"))
      {
           collision.gameObject.SetActive(false);
           // Health must be substracted from the player I suppose?
      }
 }

Where your fire method will only contain:

  void Fire()
  {
      var apple = (GameObject)Instantiate (applePrefab, appleSpawn.position, appleSpawn.rotation);
      apple.GetComponent<Rigidbody> ().velocity = apple.transform.forward * 10;
  }
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 mattkell462 · Feb 18, 2018 at 08:58 PM 0
Share

Would it be possible to create a gameobject for my player class and associate that game object with the prefab. That would help with my health subtraction code.

avatar image ShadyProductions mattkell462 · Feb 18, 2018 at 10:16 PM 0
Share

You could just tag the player gameobject with 'player' and get it in code like this:

 private GameObject player;
 
 void Start() 
 {
      player = GameObject.FindWithTag("player");
      Destroy (gameObject, 5.0f);
 }

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

118 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 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 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 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 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 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

2D Character Falling Through Ground 0 Answers

All rigidbodies fall through terrain 1 Answer

Trigger Sets GameObject 0 Answers

Trouble with Parenting gameObjects and BoxColliders 1 Answer

Detect if Raycast hits a game object 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