- Home /
Question by
Mannfred · Feb 08, 2013 at 09:13 AM ·
gameobjectcollidertrigger
Struggle with vollider detection
I have a problem that I don't know how to fic.. I'm a noob when it comes to c#. I want a collider to detect a specific gameobject called "stones", not every object that collides with it. here is the code i have written so far:
using UnityEngine;
using System.Collections;
public class stonecollider : MonoBehaviour {
stonecounter myPlayerScript;
void Awake()
{
myPlayerScript = transform.parent.GetComponent<stonecounter>();
}
void OnTriggerEnter(Collider theCollision)
{
GameObject collisionGO = theCollision.gameObject;
myPlayerScript.theScore++;
}
}
and the other script:
using UnityEngine;
using System.Collections;
public class stonecounter : MonoBehaviour {
public int theScore = 0;
void OnGUI()
{
GUILayout.Label("Score: " + theScore);
}
}
Comment
Answer by robertbu · Feb 09, 2013 at 07:14 AM
You can check either the name of the object, or the tag. Tagging and checking the tag is more efficient. So you would do something like:
void OnTriggerEnter(Collider theCollision)
{
GameObject collisionGO = theCollision.gameObject;
if (theCollision.name == "stones")
myPlayerScript.theScore++;
}
Your answer
Follow this Question
Related Questions
Multiple Colliders Trigger problem 2 Answers
I need help with triggers 1 Answer
Moving objects using raycasting? 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Check if trigger collider is touching other trigger collider 2 Answers