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 /
avatar image
0
Question by justinkatic · Oct 04, 2017 at 06:11 AM · collisioninstantiateprefabrandom.rangeitem

Item Drops on enemy spawn and death?

so I have a health potion to drop on the enemy death when it basically collides with my other objects and the giving it a random chance 25% at the moment to drop the item (potion). but currently it is dropping on both spawn and destroy. some help would be amazing :D

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DestroyAI : MonoBehaviour { public GameObject Healthpot; public float dropRate = 0.25f; //25% drop chance public Transform HealthPotSpawn;

 void OnCollisionEnter (Collision col)
 {
     if (col.transform.tag == "Bullet")
         Destroy (gameObject);
     else if (col.transform.tag == "Player")
         Destroy (gameObject);
     else if (col.transform.tag == "Enemy")
         Destroy (gameObject);
     else if (col.transform.tag == "Gooba")
         Destroy (gameObject);
 

         if (Random.Range (0f, 1f) <= dropRate)
             Instantiate (Healthpot, HealthPotSpawn.position, HealthPotSpawn.rotation);
     }

}

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 Dragate · Oct 04, 2017 at 07:21 AM 0
Share

I think enemy must be hitting something other than Bullet,Player,Enemy,Gooba when it spawns. Could check what it hits with:

 Debug.Log(col.gameObject.name);

If my assumptions are right, you could move your Instantiate inside the if statements.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tu-Le · Oct 04, 2017 at 07:19 AM

look where you place your drop function

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
avatar image
0

Answer by calebheale · Oct 04, 2017 at 07:36 AM

You need curly braces around the last "else if" response.

 void OnCollisionEnter (Collision col)
  {
      if (col.transform.tag == "Bullet")
          Destroy (gameObject);
      else if (col.transform.tag == "Player")
          Destroy (gameObject);
      else if (col.transform.tag == "Enemy")
          Destroy (gameObject);
      else if (col.transform.tag == "Gooba") { // add open curly
          Destroy (gameObject);
  
          if (Random.Range (0f, 1f) <= dropRate)
              Instantiate (Healthpot, HealthPotSpawn.position, HealthPotSpawn.rotation);
     } // add close curly
 } // fix your indenting for clarification

I would also recommend using that syntax for all "if" and "else if" statements as it helps code readability. And I would also recommend reading this article for further clarification: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else

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

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

BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer

Instantiate Prefabs on sphere surface(mini planet) 1 Answer

Collision Damage, Hit points and Instantiate 1 Answer

[SOLVED] Destroy instantiated object prefab on collision 1 Answer

Instantiating random prefabs when the player passes through a trigger 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