//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 ???
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;
}
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 ?! , 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