- Home /
How can I detect if box is touching a tag named weapon?
Sorry for the basic question, but if we have a script inside of an object with a collider, how can it react to the object with a tag named weapon? I'm not asking for help doing something with the object that has the tag, but how do I get it to detect the weapon so I can then do something with it?
Answer by clunk47 · May 21, 2013 at 05:52 PM
void OnCollisionEnter(Collision other)
{
if(other.tag == "weapon")
{
DoSomething();
}
}
Check the Script Reference for OnCollisionEnter for UnityScript and C# examples.
I'm getting an error that says a semi colon is expected after that first line you typed. Here is the portion of the script.
function Update () {
void OnCollisionEnter(Collision other){
if(other.tag == "Weapon"){
other.transform.parent = ThrowDestination.transform; //change to pickup destination
}
}
}
I only get this error: Assets\Skripte\Spieler.cs(22,19): error CS1061: 'Collision' does not contain a definition for 'tag' and no accessible extension method 'tag' accepting a first argument of type 'Collision' could be found (are you missing a using directive or an assembly reference?)
you cant call it in the update, its a function what calls when collision enters, you put it outside other functions and when it collides, it calls that function onCollisionEnter, then you do a check inside the function for the tag and so on.
As I said, this is NOT a full script. This is not tested. This is just to give u an idea of what to do.
And Bunnybomb is right, don't call a void within another void, that's like calling a function inside another function. You want void Update(), void Start(), void Awake(), void OnCollisionEnter(), etc... all separated.
Your answer
Follow this Question
Related Questions
"Player" tag collider from other GameObject? 1 Answer
Player Animation change when pickup Weapon 0 Answers
Enemy can't find weapon collider 0 Answers
specifying collider OnTriggerEnter 1 Answer
Collision detection problem 1 Answer