I’m trying to figure out a simple way to use a trigger to enable/activate 3 objects in another room.
i have a couple of scripts i’m using…
//////// this is on the trigger///////
function OnTriggerEnter (other : Collider) {
print(“In Trigger Collider”);
if (other.gameObject.tag == “kit_01”)
{
if(gameObject == gameObject.FindWithTag (“kit_01”))
{
print(“Found a bunch of game objects”);
enabled = true;
}
}
}
///////////////////////////////
So that little bit works, and everytime my game camera passes through the trigger i get the printed message.
Oddly though i never get the 2nd printed message for the “bunch of game objects” and it surely never enables anything. I’m almost 100% sure i’m doing something wrong.
I have 3 objects with a tag of kit_01 on them… with this script…
/////////////////////////
var target : Transform;
var smooth = 5.0;
function onEnable () {
transform.position = Vector3.Lerp (
transform.position, target.position,
(Time.deltaTime * .05) * smooth);
}
/////////////////////////
Any help would be greatly appreciated. I basically want anything with a tag of kit_01 to be active but not running the above script until the trigger is enabled.
(Please use code tags around your code to make it more readable.)
In the first example, your second if statement is wrong and I would remove it alltogether as I don’t see what it should help. The first if already checks the tag of the game object that collided with the trigger and that should be enough to identify it.
The second if actually checks if the trigger game object is the same as the game object tagged with “kit_01”, which would only be the case if the trigger is tagged “kit_01”.
Also, the “enabled = true;” enables the script on the trigger, which obviously already is enabled. What you want is probably:
other.gameObject.active = true;
I’m still not seeing the results i’m looking for, or actually really anything. I did take out the portion you mentioned, thanks for explaining why that wouldn’t work.
From there i just added other.gameObject.active=true; but that didn’t seem to activate said object. Perhaps i’m not quite understanding my first if statement correctly as i did get it from another script
if (other.gameObject.tag==“kit_01”)
I would assume that the game would fine all the gameObjects with kit_01 as their tag and do something on them. I’m still not even getting the print statement i have included in the if statement so i’m assuming that its either not finding anything with that tag or that its looking at whatever hits the trigger to see if it has that tag?
I don’t want the objects to be hidden, but want them to have active just not running their “attack” script, almost as if they are on pause. Seems like i could do something in the if statement that “turned on” script X?
Use
Debug.Log(other.gameObject.tag);
to check the tags of the game objects your trigger is colliding with. That will tell you if you have them set up correctly.
To enable a specific script in the if you have to do:
Ahhh the debug function is nice. That’s coming in handy already.
ok so i see that i’ve apparently misunderstood my script… basically the test checks for the tag of whatever is colliding with the trigger… so essentially other.gameObject.tag = my controller for the character. Not what i wanted.
I’m gonna have to dig a bit deeper, although i think I might be able to make something simpler w/o an if statement.
Thanks for including the bit on activating a script… i think that may better do what i’m looking for.
editing post for easier reading. *
After looking around a little more i’ve found what seems like a better way to set this up but i’m still having no positive results…
Here’s the script…
//Hide the selected GameObject
var testObject : GameObject;
testObject.active=false;
//Run through a trigger and activate all the objects w/the tag.
function OnTriggerEnter (other : Collider)
{
//print ("In Trigger Collider");
//testObject.active=true;
var tagObj = GameObject.FindGameObjectsWithTag ("betterTag");
for(var srcObj in tagObj)
{
print(srcObj);
srcObj.active = true;
}
}
Basically i get nothing from the FindGameObjectsWithTag function… it returns Unity.GameObject and not the name of the object. In addition is doesn’t activate the object. I know the collisions working because the 2 commented lines prior to the for loop work fine. For some reason i just can’t seem to get at thos tagged objects. Anyone have some insight?
I got it working by rearranging the stuff around. Here’s the final result.
//Hide the selected GameObject
var tagObj : GameObject[];
tagObj = GameObject.FindGameObjectsWithTag("betterTag");
//Doing this because i'm lazy. ultimately will run these through a startup loop.
var testObject : GameObject;
var testObject2 : GameObject;
testObject.active=false;
testObject2.active=false;
//Run through a trigger and activate all the objects w/the tag.
function OnTriggerEnter (other : Collider)
{
for(var srcObj : GameObject in tagObj)
{
print(srcObj.name);
srcObj.active = true;
}
}
It appears that i was defining tagObj inside the loop was causing a problem. not sure exactly why that would be the case but by pulling it out it works.
Thanks for the help and i appreciate any comments anyone may have.