Het
Het

Reputation: 203

Error Destroying Object

public class NewBehaviourScript : MonoBehaviour 
{   
    private string hitobject;

    void Update () {
        if (Input.GetButtonUp ("Jump")) {
            Debug.Log("SPACE");
            Rigidbody r = GameObject.Find("trigger").AddComponent<Rigidbody>();
            r.mass = 3f;
        }
    }
    void OnCollisionEnter(Collision myCollision)
    {
        hitobject = myCollision.gameObject.tag;
        if (hitobject == "wall")
        {
            Destroy(hitobject);
        }
    }
}

Whats the problem?

Error says:

Assets/NewBehaviourScript.cs(20,25): error CS1502: The best overloaded method match for `UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments

Assets/NewBehaviourScript.cs(20,25): error CS1503: Argument #1' cannot convertstring' expression to type `UnityEngine.Object'

Upvotes: 2

Views: 590

Answers (2)

Milad Qasemi
Milad Qasemi

Reputation: 3059

we changed string to gameobject so we can destroy the gameobject not a string and use the tag one of its properties(vars) to check what we hit

 private GameObject hitobject ; 
 void OnCollisionEnter(Collision myCollision)
 {
   hitobject = myCollision.gameObject;
    if (hitobject.tag == "wall")
      {
       Destroy(hitobject);
      }

}

Upvotes: 0

axwcode
axwcode

Reputation: 7824

You are trying to destroy the tag instead of the GameObject.

You can fix it like this:

void OnCollisionEnter(Collision myCollision)
{
    GameObject g = myCollision.gameObject;        

    if(g.tag == "wall")
       Destroy(g);
}

Upvotes: 2

Related Questions