I’m trying to make it so that if I have two cubes one has a empty attached called Value Level 1 which equates to 1 point(s) in my game. The other will will have an empty attached to it called Value Level 2 which equates to 2 points in my game. If you move the cube into the trigger zone it will give you the points then delete the object.
Problem being is that if I try to use two tags like so:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TriggerZone2 : MonoBehaviour {
public Text MyText;
private int score;
// Use this for initialization
void Start () {
MyText.text = "";
}
// Update is called once per frame
void Update () {
MyText.text = "$" + score;
}
void OnTriggerEnter(Collider coll) {
if (coll.CompareTag ("Pickable") && coll.CompareTag ("ValueLevel1")){
score = score + 1;
}
if (coll.CompareTag ("Pickable")) {
Destroy (coll.gameObject);
}
}
}
It won’t work, but if I do it with just the one tag it works:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TriggerZone2 : MonoBehaviour {
public Text MyText;
private int score;
// Use this for initialization
void Start () {
MyText.text = "";
}
// Update is called once per frame
void Update () {
MyText.text = "$" + score;
}
void OnTriggerEnter(Collider coll) {
if (coll.CompareTag ("Pickable")){
score = score + 1;
}
if (coll.CompareTag ("Pickable")) {
Destroy (coll.gameObject);
}
}
}
Both of these do not throw any errors in the console. The first works, but the second doesn’t. This is really bugging me.
That’s why I made an empty, tagged it, and parented it to the other object. Effectively giving it two tags (in a sense). The game runs without errors, but the score doesn’t increase it just deletes the objects once it enters the zone.
Objects simply can’t have two tags, though. GameObject.tag is a string, and a string can’t have two different values. Making a child with a different tag just means you have two objects with different tags, not one object with two tags. It would probably be easiest if you made your own system for multiple tags. Or you could get one from the asset store, like this (free).
That’s what I was trying to say, but as you can see HERE I have it setup like that already. However it doesn’t work. I don’t know why it won’t work though.
Making child objects doesn’t somehow magically change the way Unity works. One tag per object, no exceptions. The only way around that is to bypass Unity’s tag system entirely and implement your own. I think that would be cleaner than empty child objects anyway.
Well I tried using Tag Frenzy, and it seems to work fine, but same problem as before. I am able to pickup my object, it gets deleted like it should, but it doesn’t add to the score counter.
As I mentioned, the only way around one tag per object is to bypass the Unity tag system. Tag Frenzy can’t change the way Unity works, it uses a different system entirely. This means no Unity built-in functions involving tags will work (such as CompareTag); read the Tag Frenzy docs for alternatives.
All you need is the bit at the end about “retrieving tags from GameObjects”. Specifically GameObject.tags(), which is like Unity’s GameObject.tag, except you get a list of strings instead of a single string.
Using a dummy script with a public value would help too, and instead of the tag check, check the value in the script instead. Would save you from tag frenzy… Point being though, if that screen was tag frenzy, that’s quite neat!
That’s what Tag Frenzy already does, but with extension methods so you don’t have to manually GetComponent and stuff, you just use GameObject.tags() instead of GameObject.tag. Why make things harder?