Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Cannot pick things up!!

Discussion in 'Scripting' started by willgoldstone, Oct 13, 2006.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi everyone

    im learning unity and have made a basic car level you can drive around and score points by collecting boxes (cubes with the tag "collectMe") which works fine with the following script -

    Code (csharp):
    1. var bigBang : GameObject;
    2.  
    3. var scoreObjectName = "Score";
    4. var scoreScript : Score;
    5.  
    6. //before we do anything else, find the score object
    7. scoreObject = gameObject.Find(scoreObjectName);
    8.  
    9. if(scoreObject) {
    10.     // Now find the Score script on that object
    11.     scoreScript = scoreObject.GetComponent(Score);
    12. }
    13.  
    14. function OnCollisionEnter (collision:Collision) {
    15.  
    16.     //check and see if the collision has a tag called "collectMe"
    17.     if (collision.gameObject.tag == "collectMe"){
    18.         //add a point to your score!
    19.         if(scoreScript) {
    20.             scoreScript.score += 1;
    21.         }
    22.        
    23.         //play the sound
    24.         audio.Play();
    25.  
    26.         //make a variable for the location of the collided box
    27.         var whereExplode = collision.gameObject.transform.position;
    28.  
    29.         //instantiate an explosion using the prefab delcared as "bigBang"
    30.         Instantiate (bigBang, whereExplode, Quaternion.identity);
    31.  
    32.         //destroy the collided with object
    33.         Destroy (collision.gameObject);
    34.     }
    35. }
    36.  
    Now i'm trying to apply this to the FPS contoller prefab, but it does not work. I thought this may be because the FPS controller doesnt seem to have a collider, so i added a child cube (with its box collider) and tried it with the script attached to that, still cant pick up that damn box!

    Can anyone guess why this wouldnt work? any help much appreciated!!


    Thanks :)

    Will
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    With the charactercontroller you will have to use OnControllerColliderHit.

    However, i would suggest you use triggers for this. Pickup objects usually shouldnt collide with rigidbody or objects anyway.

    You can override function OnTriggerEnter () {} for it.

    http://unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

    Then you also need the object which is to b e picked up a trigger by checking the colliders isTrigger flag.
     
  3. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi Joachim,


    thanks for the reply, im trying to change this script using the methods you suggested and i've looked at the script reference but cant quite get how to address the cube to pick up - it didnt seem to like my use of tags / gameObject to reference things. (see my first posting of my script), ive now got this -

    Code (csharp):
    1. var bigBang : GameObject;
    2.  
    3. var scoreObjectName = "Score";
    4. var scoreScript : Score;
    5.  
    6. //before we do anything else, find the score object
    7. scoreObject = gameObject.Find(scoreObjectName);
    8.  
    9. if(scoreObject) {
    10.     // Now find the Score script on that object
    11.     scoreScript = scoreObject.GetComponent(Score);
    12. }
    13.  
    14. function OnControllerColliderHit (hit : ControllerColliderHit) {
    15.  
    16.     //check and see if the collision has a tag called "Destructible"
    17.     if (hit.collider == "Cube"){
    18.         //add a point to your score!
    19.         if(scoreScript) {
    20.             scoreScript.score += 1;
    21.         }
    22.        
    23.         //play the sound
    24.         audio.Play();
    25.  
    26.         //make a variable for the location of the collided box
    27.         var whereExplode = collision.gameObject.transform.position;
    28.  
    29.         //instantiate an explosion using the prefab delcared as "bigBang"
    30.         Instantiate (bigBang, whereExplode, Quaternion.identity);
    31.  
    32.         //destroy the collided with object
    33.         Destroy (collision.gameObject);
    34.     }
    35. }
    this gives the following errors (see pic attached)


    Thanks :)


    Will
     

    Attached Files:

  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You are using a variable called 'collision' but it is not defined anywhere.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Keli is right.

    Generally if you have an compile error, double click on the error in the console. This will bring you to the error in the script, in this case change:

    Destroy (collision.gameObject);
    to:
    Destroy (hit.collider.gameObject);

    and

    var whereExplode = collision.gameObject.transform.position;
    to:
    var whereExplode = hit.collider.transform.position;
     
  6. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Guys,


    thanks for your replies- sadly just dont quite follow what you're telling me enough to implement it... im pretty new to the software. I've now got the following -

    var bigBang : GameObject;

    var scoreObjectName = "Score";
    var scoreScript : Score;

    //before we do anything else, find the score object
    scoreObject = gameObject.Find(scoreObjectName);

    if(scoreObject) {
    // Now find the Score script on that object
    scoreScript = scoreObject.GetComponent(Score);
    }

    function OnControllerColliderHit (hit : ControllerColliderHit) {

    //check and see if the collision has a tag called "collectMe"
    if (hit.collider.gameObject.tag == "collectMe"){
    //add a point to your score!
    if(scoreScript) {
    scoreScript.score += 1;
    }

    //play the sound
    audio.Play();

    //make a variable for the location of the collided box
    var whereExplode = collision.gameObject.transform.position;

    //instantiate an explosion using the prefab delcared as "bigBang"
    Instantiate (bigBang, whereExplode, Quaternion.identity);

    //destroy the collided with object
    Destroy (hit.collider.gameObject);
    }
    }

    and the errors noted in the attached pic

    thanks for listening!
     

    Attached Files:

  7. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Nevermind, now got this, which works! -

    var bigBang : GameObject;

    var scoreObjectName = "Score";
    var scoreScript : Score;

    //before we do anything else, find the score object
    scoreObject = gameObject.Find(scoreObjectName);

    if(scoreObject) {
    // Now find the Score script on that object
    scoreScript = scoreObject.GetComponent(Score);
    }

    function OnControllerColliderHit (hit : ControllerColliderHit) {

    //check and see if the collision has a tag called "collectMe"
    if (hit.collider.gameObject.tag == "collectMe"){
    //add a point to your score!
    if(scoreScript) {
    scoreScript.score += 1;
    }

    //play the sound
    audio.Play();

    //make a variable for the location of the collided box
    var whereExplode = hit.collider.gameObject.transform.position;

    //instantiate an explosion using the prefab delcared as "bigBang"
    Instantiate (bigBang, whereExplode, Quaternion.identity);

    //destroy the collided with object
    Destroy (hit.collider.gameObject);
    }
    }