- Home /
Variable doesn't change when box hit trigger area
I'm trying to make a trigger area change value after a specific object (cube) enters the area (cube trigger), but when I move the cube to the area, the "debug" value doesn't change (from 1 to 2). What am I doing wrong?
var other : GameObject;
//var fuel_points : int = 100;
var debug : int = 1;
function OnCollisionEnter (other : Collision) {
if(gameObject.tag=="other"){
debug ++;
//Destroy(other.gameObject);
//fuel_points = other.GetComponent(fuel_points);
//fuel_points += 10;
//}
//else(game do blablablabla)
// Destroy(other.gameObject);
}
}
Ok, to avoid confusion if I can, I'm going to refer to the object as Cube and the area as Area. You have your script attached to Area and you are wanting your code to trigger when Cube enters Area. Your code is looking for an object that is tagged as "other", can you confirm that Cube has an "other" tag?
Hey Trucido, Just following up on this, any progress? Did any of the answers help you?
Answer by aldonaletto · May 30, 2013 at 08:35 PM
gameObject.tag is the tag of the object to which this script is attached, and you should use OnTriggerEnter instead:
function OnTriggerEnter (other : Collider) {
// verify whether the tag of the object entering the trigger is "other":
if (other.tag=="other"){
debug ++; // if so, increment debug
}
}
NOTE: This script is comparing the entering object's tag to "other" - is this what you actually want to do? Usually the player is tagged "Player", but of course you can use any tag you want - provided that you have registered this tag previously.
Answer by Trucido · May 30, 2013 at 09:04 PM
Alright, thank you for your answers but this is how it works:
Picking up a cube with a certain script (the one I posted) and releasing it into the trigger area.
The cube is a catalyst that will increase the int variable
Problem is, when it collides it doesn't change, yes I've selected the correct "other" and the pickup is same target, but is there a chance that they are colliding with each other? It doesn't show up any errors while doing this.
Please post responses as comments on the associated answer, or as a comment on your question if it is a general statement. Answers are for answering.
Back to your problem though: gameObject.tag == "other"
is not going to work because it will either always be true or always be false unless you are dynamically changing tags. $$anonymous$$y recommendation would be to A. Use a more informative tag, "other" is very confusing in this context. and B. check the tag of the object that you collided with, rather than your own tag. Aldonaletto's example is best practice for this.
Answer by Memige · May 30, 2013 at 08:35 PM
Edit 2**
Based on your response I'm gathering that what you are doing is that your collision script is attached to the area, and it is listening for collision of these cubes that will give it fuel. toward that end I would propose the following restructure: Create a new Tag called "Fuel" assign the cubes this new tag, then swap out your current code for the following:
var debug : int = 1;
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Fuel")
{
debug++;
//DoMyAwesomeLogic();
}
}
Edit** Aldonaletto's response is a little more concise and technically more correct, I'd recommend his approach.
Original**
Okay, actually I just took a second look and here are the two things I'm seeing. As I mentioned in my comment, you are looking for an "other" tag, please make sure you are not confusing this with your other: Collision
variable. Secondly, you are not checking the collided object but rather the object the script is on, try changing
if(gameObject.tag=="other")
to
if(other.gameObject.tag=="other")
and as mentioned, make sure that you have an "other" tag on the cube