- Home /
How to check if a tag name contains something.
I have this pick up script and the following line that doesn't work:
(objectToPickup.tag != "Carryable" | objectToPickup.tag != "Meat")
So I decided to use a different method: Making the tag names Carryable Meat, Carryable Vegetables etc. and checking if the tag names contain the word "carryable". How do I go about to doing this?
Any help is appreciated.
Answer by efeguclu · Jul 04, 2017 at 10:32 AM
Try this out :
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name.Contains("Carryable"))
{
}
}
Answer by Vollmondum · Jul 03, 2017 at 08:40 PM
Use tags for general category only. E.g. "collectible" Add that one general tag to everything you can collect.
Thus you might want this instead of yours.
Create a script "itemPrefs" with a collectible info: name, cost, whatever else you need. On collision, reference the script with all info you want, from the collided collectible.
That will spare you the hassle of 1000 tags.
What do you mean by "On collision"? Do you mean the method?
function OnTriggerEnter(col:Collider)
{
if(col.tag == "collectible")
{
var thisItemName = col.GetComponent(itemPrefs).itemName;
PlayerPrefs.SetInt(thisItemName, PlayerPrefs.GetInt(thisItemName) + 1);
Destroy(col.gameObject);
}
}
Boom, collected.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
CompareTag being ignored? 1 Answer
On raycast hover change crosshair image 1 Answer
How to implement a low pass filter for accelerometer? 2 Answers