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
-1
Question by DricoJD · Feb 12, 2014 at 03:50 PM · collisiondestroycollidersfunctions

Unity not recognising Destroy()

Hello, Unity is giving me these two errors and not recognising Destroy()

error CS1503: Argument #1' cannot convert string' expression to type UnityEngine.Object' > error CS1502: The best overloaded method match for UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class ContactWithPlayerKill : MonoBehaviour 
 {
     void OnCollisionEnter (Collision col)
     {
         if (col.gameObject.tag == "Player")
         {
             KillPlayer();
         }
     }
 
     void KillPlayer ()
     {
         Destroy("Player");
     }
 
 }
 
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

2 Replies

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

Answer by Jeremy2300 · Feb 12, 2014 at 03:56 PM

"Player" is simply a string, not an object reference. Try doing this instead:

 public class ContactWithPlayerKill : MonoBehaviour 
 {
     void OnCollisionEnter (Collision col)
     {
        if (col.gameObject.tag == "Player")
        {
          KillPlayer( col.gameObject );
        }
     }
  
     void KillPlayer ( GameObject DestroyMe )
     {
        Destroy( DestroyMe  );
     }
 }
Comment
Add comment · Show 14 · 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 iamvishnusankar · Feb 12, 2014 at 04:02 PM 0
Share

gameObject & GameObject are different. In your script you're passing an instance which will result in Null Referance Exception

avatar image Jeremy2300 · Feb 12, 2014 at 04:14 PM 1
Share

Oops, guess I had a small typo. Fixed it :)

avatar image DricoJD · Feb 12, 2014 at 07:36 PM 0
Share

The Code does not have any errors but I am afraid that the player is still not getting destroyed. I would like to point out the script is on the object = Water ins$$anonymous$$d of the object = player

avatar image DajBuzi · Feb 12, 2014 at 07:42 PM 0
Share
  1. You want to destroy player object when going into the water?

  2. Do you have colliders on the player game object and on water gamo object?

  3. Any of yhose 2 objects have isTrigger box checkd (if so then with one)

  4. Any more details that we should know?

avatar image DricoJD · Feb 12, 2014 at 09:37 PM 0
Share
  1. Yes Destroy the gameObject Player when hits the water with the collider that has trigger set already.

  2. Collider on the Player and a Trigger Collider on water.

  3. Yes the water object(s).

  4. That is all.

Show more comments
avatar image
2

Answer by iamvishnusankar · Feb 12, 2014 at 03:58 PM

Try this script. Same as your script with some edits :)

using UnityEngine; using System.Collections;

 public class ContactWithPlayerKill : MonoBehaviour 
 {
     void OnCollisionEnter (Collision col)
     {
        if (col.gameObject.tag == "Player")
        {
          KillPlayer(col.gameObject);
        }
     }
  
     void KillPlayer (GameObject pPlayer)
     {
        Destroy(pPlayer);
     }
  
 }
Comment
Add comment · Show 5 · 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 DricoJD · Feb 12, 2014 at 07:22 PM 0
Share

The Errors are now fixed, thanks. However, when I contact the object I am colliding with. Nothing happens. Where is the reference to pPlayer I am slightly confused on that part.

avatar image DajBuzi · Feb 12, 2014 at 07:25 PM 1
Share

You dont need reference since it is declared as method parameter wich is filledby OnCollisionEnter method.

avatar image iamvishnusankar · Feb 12, 2014 at 09:14 PM 0
Share

The pPlayer game object is the col.gameObject. As we're just passing the object reference to $$anonymous$$illPlayer() function as function argument.

If you're still having issue in destroying game object, use OnTriggerEnter method ins$$anonymous$$d of OnCollisionEnter method. $$anonymous$$ake your collider a trigger collider just by checking "IsTrigger" on inspector menu.

void OnTriggerEnter(Collider col) {

 //your logic here

 } 
avatar image DricoJD · Feb 12, 2014 at 09:26 PM 0
Share

Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.

avatar image iamvishnusankar · Feb 12, 2014 at 09:42 PM 0
Share

Was a spell error as I misspelled "Collided" ins$$anonymous$$d of "Collider" (Poor AutoCorrect :D & Now fixed). Error happens since am replying you from my phone and it's 4.0Am in our country. Wizards need sleep too :D ;)

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

24 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

Related Questions

Target touching enemy collision 1 Answer

onCollision Destroy 1 Answer

Why isn't my Death Script Working? 3 Answers

Destroy the gameObject after seconds when not hit. 0 Answers

My player is suddenly and rarely destroyd when collecting coins 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