Search Unity

Getting a gameObject to destroy when two other gameObjects collide

Discussion in 'Scripting' started by Kmporter15, Sep 15, 2014.

  1. Kmporter15

    Kmporter15

    Joined:
    Sep 15, 2014
    Posts:
    5
    I am running into a problem with getting a gameObject to destroy when two other gameObjects collide. I am making a simple game where bunnies eat a player’s carrot from their garden, when a bunny reaches and touches (collides) with the garden, the player loses a carrot. I have that part working as intended, but when I try to switch to having an actual 2D carrot get destroyed instead of in the GUI, I am unable to do so. So basically I want the bunny to hit the garden and subtract/destroy the carrot gameObject from the garden

    Script for the bunny:

    function OnTriggerEnter2D(other:Collider2D)
    {
    if(other.gameObject.tag =="Garden")
    {
    //Resets the Bunny
    transform.position.y = -6;
    transform.position.x = Random.Range(3, -4);
    }


    if(other.gameObject.tag =="Garden")
    {
    other.GetComponent("Garden_Script").carrots-= 1;
    SceneManager.transform.GetComponent("SceneManagerScript").SubtractCarrot();
    }


    }


    Script for the carrots:


    function OnGUI ()
    {
    GUI.Label(Rect(20,20,100,20), "Carrots:" + carrots);
    }

    function SubtractCarrot()
    {
    carrots -= 1;
    }



    Additional code for carrots:


    function OnCollisionEnter2D (coll: Collision2D)
    {
    if(collider.gameObject.tag =="Bunny")
    {
    Destroy(gameObject.find("carrots"));


    }
    }
     
    Last edited: Sep 15, 2014
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    is the carrot object you're trying to destroy named "carrots"? or did you meant to look for a game object tagged "carrots"?
    Also might want to try changing gameObject.Find("carrots") to GameObject.Find("carrots"). Sometimes a captial G makes all the difference! ;) Hope this helps.
     
  3. Kmporter15

    Kmporter15

    Joined:
    Sep 15, 2014
    Posts:
    5
    That existing code works the way I want it to, but now instead of subtracting a carrot from the GUI the way I have it set up now, I want an actual gameobject to be visually destroyed from the screen. Right now, when the garden gets hit, carrots are deducted from the GUI using the OnGUI function. What Im trying to do is make it so that when the garden is hit, an actual gameobject is destroyed. !0 carrots to be on screen to be exact, with one being destroyed every time a bunny collides with the garden
     
  4. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    ok my idea is if you have a list of your carrot objects (that are in your garden) in this script, and when you subtract a carrot, you destroy an object.

    Here's an example of what I mean:

    //these two lines would go at the top of your carrots script with the SubtractCarrot function
    import System.Collections.Generic;
    public var carrotObjects : List.<GameObject>;

    //these lines would go inside your SubtractCarrot function
    GameObject carrot = carrotObjects[0];
    GameObject.Destroy(carrot);
    carrotObjects.Remove(carrot);


    So basically you create a list of all the carrot objects, and for every carrot you subtract you remove one from the scene, and then also remove the carrot from the list. Hope this helps and makes sense! :)
     
  5. Kmporter15

    Kmporter15

    Joined:
    Sep 15, 2014
    Posts:
    5
    Thanks for the help TwixEmma. Im actually getting an error for the import System.Collection.Generic; now. The error says "BCE0044: expecting EOF, found 'import'".
     
  6. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    check your code to see if you have one too many brackets '(' & ')' or one too many braces '{' & '}'
     
  7. Kmporter15

    Kmporter15

    Joined:
    Sep 15, 2014
    Posts:
    5
    I think maybe I'm just over looking something here

    staticvarcarrots : int = 10;
    importSystem.Collections.Generic;
    publicvarcarrotObjects : List.<GameObject>;

    functionUpdate ()
    {
    if(carrots <= 0)
    {
    Application.LoadLevel("Lose");
    carrots = 10;
    }

    }

    functionOnGUI ()
    {
    GUI.Label(Rect(20,20,100,20), "Carrots:" + carrots);
    }

    functionSubtractCarrot()
    {
    carrots -= 1;
    GameObject.carrot = carrotObjects[0];
    GameObject.Destroy(carrot);
    carrotObjects.Remove(carrot);
    }
     
  8. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    Code (JavaScript):
    1. import System.Collections.Generic;    //import lines go right at the top always
    2.  
    3. static var carrots : int = 10;
    4. public var carrotObjects : List.<GameObject>;
    5.  
    6. function Update ()
    7. {
    8.     if(carrots <= 0)
    9.     {
    10.         Application.LoadLevel("Lose");
    11.         carrots = 10;
    12.     }
    13.  
    14. }
    15.  
    16. function OnGUI ()
    17. {
    18.     GUI.Label(Rect(20,20,100,20), "Carrots:" + carrots);
    19. }
    20.  
    21. function SubtractCarrot()
    22. {
    23.     carrots -= 1;
    24.     var carrot = carrotObjects[0];    //C# converter to JS, my bad (TwixEmma)
    25.     GameObject.Destroy(carrot);
    26.     carrotObjects.Remove(carrot);
    27. }
    28.  
    Hope this helps! :)
     
  9. Kmporter15

    Kmporter15

    Joined:
    Sep 15, 2014
    Posts:
    5
    Thank you, I really appreciate the help. I have one more question about the list. I just want to make sure Im clear on what Im doing. For the list, I am tagging the tagging the carrotObjects as carrots right?