how can i except object from others ???

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 ???

You could attach a behaviour to your objects which provides the score rewarding logic:

using UnityEngine;

public class ScoreRewarder : MonoBehaviour {
    public int score;
}

And then:

private void OnTriggerEnter2D(Collider2D collisionObject) {
    // Reward score (if possible)!
    var scoreRewarder = collisionObject.GetComponent<ScoreRewarder>();
    if (scoreRewarder != null)
        Score += scoreRewarder.score;

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

please where should i put 9 lines code ?

Another quick fix to this (but less elegant solution) would be to tag each different object and behave differently according to the tag

void OnTriggerEnter2D(Collider2D collisionObject)
{

if (collisionObject.gameObject.Tag == "yourObjectTag")
{
     Score += 2;
}

if (collisionObject.gameObject.Tag == "yourOtherObjectTag")
{
     Score += 5;
}

Destroy (collisionObject.gameObject);
scoreText.text= "Score " + Score;

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

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).

if (collisionObject.gameObject.CompareTag("yourObjectTag")) {
    Score += 2;
}

Didn’t know about this, thanks for saying it, is there any major difference except this between the two functions ?

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 ??

Personally, this is what I would do.
On each collectable object, this script:

public class CollectableObject : MonoBehaviour
{
     public Int scoreValue = 2;
}

Then, in your collision trigger, you just have to do this:

score += collisionObject.scoreValue;

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.

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 ?! :confused: , 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