How to perform a collider check tag

The script is attached to the collider game object and when the player is within the object, it will then check what tag the object itself is holding.

My current script
please note the script is wrong

	void OnTriggerStay(Collider other){

		if (other.gameObject.Tag == "Player") 
		{
			Debug.Log ("Success!");
		}
	}

Im trying to make a srcipt to check what type of collision the placer has entered.

Something like this…

Player enters collision, is collision lava? no, is collision an item? no, is collision a workspace (crafting) yes! ok do action!

Im using tags to accomplish this.

I dont know what you mean.

Is the script you posted attached to an object with a tag ‘xxx’ and the same script is attached to an object with tag ‘yyy’ and you want to check if the player enters this collider and then does something based on the objects tag.

The script is to be attached to the object the player passes into. and yes, the collider will do something based on the objects tag.

The script is all wrong i know

Sorry I took so long to reply I was eating some juicy roast chicken.

Will the objects tag change or will you create an object with a tag ‘lava’ and it will always be tagged as ‘lava’ ?

If the object will always have the same tag the just put a script on each object for example the lava could have lava.cs attached and workspace could have workspace.cs attached then each script can be written especially for that type of object.

What I do is create prefabs for objects and this allows you to alter and develop each object as its own seperate unit.

For example your lava could be a prefab with the lava model or sprite, its collider (set as is trigger) and the lava.cs.

Then for the script you would use something like:

using UnityEngine;
using System.Collections;

public class lava : MonoBehaviour
{
	void OnTriggerStay(Collider col)
	{
		if(col.gameObject.tag == "Player")
		{
			// do whatever the lava does to the player such as reduce player health or shield
		}
	}
}

Then on the workspace object you would attach the workspace.cs script:

using UnityEngine;
using System.Collections;

public class workspace : MonoBehaviour
{
	void OnTriggerStay(Collider col)
	{
		if(col.gameObject.tag == "Player")
		{
			// do whatever the workspace does to the player such as crafting or trading
		}
	}
}
1 Like

Ah now i got what i want, turns out i needed to attach the script to the player and make a few mods, im very certain it will work…but it dosnt…it should be scripted correctly…and the collision trigger is ticked but not working…

	void OnTriggerStay(Collider col){

		Debug.Log("Enter");
		
		if (col.gameObject.tag == "Item") {
				Debug.Log ("This is an item!");
		}
		if (col.gameObject.tag == "Tree") {
				Debug.Log ("This is a tree!");
		}
		if (col.gameObject.tag == "Smelt") {
				Debug.Log ("This is a smeltery!");
		}

I have made a custom made actor so i assume its to do with him/her

The object the script is attached to needs a rigidbody and ‘is trigger’ not ticked.

The objects colliding with it (item, tree, smelt) need a collider and the ‘is trigger’ needs to be ticked.

Apparantly your way is not the correct approach, you might find this thread helps.

http://forum.unity3d.com/threads/216802-Which-method-is-the-least-resource-intensive-for-collision-detection

1 Like