- Home /
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 typeUnityEngine.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");
}
}
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 );
}
}
gameObject & GameObject are different. In your script you're passing an instance which will result in Null Referance Exception
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
You want to destroy player object when going into the water?
Do you have colliders on the player game object and on water gamo object?
Any of yhose 2 objects have isTrigger box checkd (if so then with one)
Any more details that we should know?
Yes Destroy the gameObject Player when hits the water with the collider that has trigger set already.
Collider on the Player and a Trigger Collider on water.
Yes the water object(s).
That is all.
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);
}
}
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.
You dont need reference since it is declared as method parameter wich is filledby OnCollisionEnter method.
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
}
Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.
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
Follow this Question
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