Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how can i except object from others ???

Discussion in 'Scripting' started by karammaks, Apr 19, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Hi

    i am working on 2d game , the player will collect some objects, and he get score for each one 1+ as this code

    void OnTriggerEnter2D(Collider2D collisionObject){


    //remove the object from gameplay
    Destroy (collisionObject.gameObject);



    Score=Score+1;


    scoreText.text= "Score " + Score;
    , but i want a way to control this , so for some objects the player get different score or he lose score -1 , please can you help me ???
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You could attach a behaviour to your objects which provides the score rewarding logic:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ScoreRewarder : MonoBehaviour {
    5.     public int score;
    6. }
    7.  
    And then:
    Code (csharp):
    1.  
    2. private void OnTriggerEnter2D(Collider2D collisionObject) {
    3.     // Reward score (if possible)!
    4.     var scoreRewarder = collisionObject.GetComponent<ScoreRewarder>();
    5.     if (scoreRewarder != null)
    6.         Score += scoreRewarder.score;
    7.  
    8.     // Remove the object from gameplay.
    9.     Destroy(collisionObject.gameObject);
    10. }
    11.  
     
    Last edited: Apr 19, 2014
  3. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    please where should i put 9 lines code ?
     
  4. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Another quick fix to this (but less elegant solution) would be to tag each different object and behave differently according to the tag
    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D collisionObject)
    3. {
    4.  
    5. if (collisionObject.gameObject.Tag == "yourObjectTag")
    6. {
    7.      Score += 2;
    8. }
    9.  
    10. if (collisionObject.gameObject.Tag == "yourOtherObjectTag")
    11. {
    12.      Score += 5;
    13. }
    14.  
    15. Destroy (collisionObject.gameObject);
    16. scoreText.text= "Score " + Score;
    17.  
    note that with multiple different objects it would be much more readable to place a switch statement instead. Then if you got more than let's say 20 different objects that have different scores, for readability sakes performance love it would be the case to use Events / manager-listener patterns
     
  5. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Using tags is okay, but I would recommend using the CompareTag method since it avoids unnecessary string allocations (each time you access the tag properties of a game object it allocates a new string).
    Code (csharp):
    1.  
    2. if (collisionObject.gameObject.CompareTag("yourObjectTag")) {
    3.     Score += 2;
    4. }
    5.  
     
  6. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Didn't know about this, thanks for saying it, is there any major difference except this between the two functions ?
     
  7. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    thank you both very much, but should i put the name of the prefab object in the "yourObjecTag" ? or what should i put instead of it to recognize a certain object ??
     
  8. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Personally, this is what I would do.
    On each collectable object, this script:
    Code (csharp):
    1.  
    2. public class CollectableObject : MonoBehaviour
    3. {
    4.      public Int scoreValue = 2;
    5. }
    6.  
    Then, in your collision trigger, you just have to do this:
    Code (csharp):
    1.  
    2. score += collisionObject.scoreValue;
    3.  
    This has the advantage of letting you use the inspector to set the value of a collectable to a custom amount, in case you have a coin that's worth considerably more. Plus, you can change the value of a collectable item through a script. All you need to add is a check to make sure the item has this variable without getting a null value exception. I'll leave that to you as a bit of homework. There's several ways to go about it.
     
  9. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    good idea but it didnt work because it didnt recognize scoreValue in the line score += collisionObject.scoreValue;

    and i still couldn't tag the object ?! :/ , should i put the prefab name of the object in the " " , or what should i write here ? help me how to tag the objects please
     
    Last edited: Apr 20, 2014